0

I have an issue - I should update information for user as soon as possible, but i don't know exact time when it'll happen.

I use setInterval function that checks differences between current state and the state before checking. If there are any differences then I send an AJAX request and update info. Is it bad? I can't (or don't know how to) listen any events in that case.

And what about interval time? All users (~300 at the same time) are from local network (ping 15-20 ms). I have to refresh information immediately. Should I better use 50ms or 500ms?

If the question is not very clear just ask - I'll try to say it in other words. Thanks in advance

Shomz
  • 37,421
  • 4
  • 57
  • 85
MIchael
  • 15
  • 2
  • _"does it good"_, please clarify – scniro Mar 09 '16 at 21:21
  • 4
    Have you considered a websocket library or framework? This would allow for the user agent to be notified via event from the server. This would allow for a perferect solution to what you are wanting. What technology is your backend? – abc123 Mar 09 '16 at 21:22
  • 1
    @abc123 why don't you write that as an answer? That's the proper approach. – Shomz Mar 09 '16 at 21:23
  • 1
    @Shomz done, I was hoping to get his backend technology so that I could write a more detailed answer. Thank you though for being fortright with me. – abc123 Mar 09 '16 at 21:28
  • For me real-time = websocket – Timo Mar 09 '16 at 21:29
  • Related question: [Ajax push system](http://stackoverflow.com/questions/7594425/ajax-push-system?lq=1) and a duplicate: [Constantly Querying Server via Javascript - Good Idea?](http://stackoverflow.com/questions/1027700/constantly-querying-server-via-javascript-good-idea?lq=1) – Yogi Mar 09 '16 at 21:56

1 Answers1

2

Solution: Websocket

Websockets allow client applications to respond to messages initiated from the server (compare this with HTTP where the client needs to first ask the server for data via a request). A good solution would be to utilize a websocket library or framework. On the client you'll need to create a websocket connection with the server, and on the server you'll need to alert any open websockets whenever an update occurs.

The issue with interval

It doesn't scale, you could set the interval to 4000 miliseconds and still once you hit 1000 users...you are going to be slamming your server with 10000 requests and responses a minute...This will use tons of data and use processing to return nothing. Websockets will only send data to the client agent only when the event you want to send actually occurs.

Backend: PHP

Frameworks

Simply implement one of the above frameworks as a websocket connection then you will register as a client to this endpoint and it will send data on whatever event you define.

abc123
  • 17,855
  • 7
  • 52
  • 82