1

I am developing a website where client needs that any notification should reach as soon as it is created. so i am using setinterval function of jquery and using ajax requests to get the notifications. the time interval I set is 2 seconds. and its not the only ajax request which is going this way. there are following ajax request being done within interval of 2 sec

  1. get notifications
  2. get messages.
  3. get counts.
  4. some other checks

I am worried because i think sending this much request at very short time period may disturb the system. and worse if the number of users increase. Please tell me your opinions and solutions to this if this is wrong aproach

5
  • en.wikipedia.org/wiki/WebSocket
    – Walfrat
    Commented Jan 5, 2017 at 8:39
  • 2
    Instead of setInterval you might want to only start the next waiting period once the previous call finished (failed or succeeded), otherwise you might run into problems. Besides that, yes, websockets are probably better suited for this, although they are (in my opinion) significantly more complicated and might not be necessary for your goals.
    – ASA
    Commented Jan 5, 2017 at 9:19
  • @Traubenfuchs Yeah I've heared about websocket but have never worked with. but First of all tell me if there is any flaw in my current apporach. I want to make clear that all these requests get the data that is min 2 letters and max 20 letters long. it can be a simple number like 1 or max a string saying "someone replied to your message." not anything more than this Commented Jan 5, 2017 at 9:28
  • Basically it's perfectly fine, but websockets might scale better. Just do the math on how many requests are possible with your expected user numbers (Take multiple tabs/windows into account!) and do some benchmarking.
    – ASA
    Commented Jan 5, 2017 at 9:33
  • I think I need to learn how tpo use websockets Commented Jan 5, 2017 at 9:47

1 Answer 1

1

I would recommend moving to websockets when possible, but until then, here are a few things to consider.

  1. Make sure you are collecting statistics on numbers of requests, and response times. While not an optimal situation, this could turn into a nightmare for you from a simple javascript error adding an additional 2 second call with each request, a single hung request, or a DDOS attack.
  2. @Traubenfuchs advice is solid. Use setTimeout and only schedule a call once you get a response.
  3. Unless there is a pressing reason, cache the results on the server side for a short period of time to prevent overwhelming your datastore.
  4. Combine all the calls into a single call is possible.
1
  • Can't stress #1 enough. I've had setInterval AJAX requests take down load balancers and web servers before when requests starting running slow. Never send an AJAX request using setInterval!! Commented Jan 6, 2017 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.