zwitterion zwitterion - 1 year ago 58
JSON Question

Is it possible a Server-Sent Events to overload the browser in any way?

I am working in a project that is using SSE aproach to push data from server to the browser each 0,5 seconds.

It grabs data from Mysql transform it in JSON and sends it to the browser.

I was wondering in an extreme scenario, where the browser will be working for some months (it is possible). The server sends to the browser 2.764.800 JSON per day. That will be 82.944.000 push per month.

So is there something to be concerned in this scenario? I mean, is there any way where the browser will be overloaded or the client system by such amount of data?

Is there any scenário that could justify a page refresh every X seconds?

Answer Source

Without some more specific details, the answer is "maybe, but probably not [a need to have a page refresh]".

As far as I know, there is no particular limit in the SSE side of things. So, in the quite typical example of receiving a data item from the server, updating the value in a div, and not storing the received data, there should be no growth in memory. Twice a second is also fine.

If you are storing data (e.g. to graph the last N values), you need to make sure values older than the last N are thrown away regularly.

My concerns would be everywhere else:

  • Browser bugs
  • Javascript memory management. (E.g. unintentional circular dependencies, that the garbage collector cannot handle.) (This looks a good discussion on this theme; another one here.)
  • Unreliable internet - some server or router along the way deciding to close a socket that has been open a long time. (SSE should just auto-reconnect when that happens though - I prefer to also add some keep-alive messaging on top, as sometimes sockets get closed un-cleanly.)

You might also ask yourself how long people will keep their browsers open, and if this is even something you need to worry about.

In trading applications, I find the close of market (whether daily, or Friday evening) is a good point to deliberately close the socket.