I'm building a system where I have 2 groups, lets call them users and consumers. Any user can send a message to any consumer. How they do this is not important but what happens is that a message is placed in a database on a server and whenever this happens a notification should be sent to the specific consumer(s) of interest (determined by the user).
So say we have a 100 consumers, then there will probably be a 1000 users. A consumer NEEDS to be notified almost instantly (max 5 seconds) whenever a user sends a message to him/her. Consumers' interface is a standard angular website and I'm using tomcat to host it, but this is not crucial and could be switched to Node or likewise if necessary.
Now what I'm trying to figure out is the best way to notify a consumer that a message has arrived. What I've considered is websockets, SSE, Long-polling and plain pull-scheme making an ajaxrequest every 5 seconds. I think all of the options are doable, but there are some further challenges:
- consumers are logged in (ideally in one session) around 18 hours a day.
- Hardware is limited (Can't afford that much that's why I'm trying to optimize :) )
- consumers probably only get new message(s) every 10 minutes or so but when they do they NEED to get the message(s) almost instantly (<5 seconds)
This presents a problem of security as far as I see (Logged in to same session in a browser 18 hours a day could be problematic I guess ??
Regarding the hardware, the only thing I reckon is that sending an ajax request to the server every 5 seconds for each consumer quickly becomes (unnecissarily) expensive if you have, say a 100 consumers as in the example above.
I don't know how expensive it is to have websockets runnning this long and if it is perhaps cheaper to make a long polling request until a response comes in and then make a new one afterwards ??
Finally I don't really understand how SSE works. Is a connection open from the server to the client(s) and if so what is the difference between SSE and long-polling ??
Any input, ideas, recommendations ?? Furthermore any links to articles, blogposts, books etc. about client-server interaction in terms of hardware capacity/usage would be very welcome as I would like to read up on this myself but have a hard time finding good hardware information on these things.
If any elaboration is needed please just ask :)
For the case where latency should be less than 5 seconds, but new messages only likely every 10 minutes:
The first three keep a dedicated socket open, but only have the overhead of connecting once (long-poll has the overhead of connecting every 10 minutes, but that will never be a system bottleneck), whereas ajax polling has the overhead of making a new connection every N seconds, and has an average of N/2 second latency.
The downside of websockets, see and long-poll is they keep a socket open. I don't see a security issue there, but you do have a potential performance issue, as each socket will be using resources on the server. But with 100 users, I think an average cloud server instance will cope fine.
Think of SSE as either a simplified websockets designed for exactly your use-case, or as a standardized version of long poll that also saves the reconnect when you get data. Hint: SSE is the technology you should go for, with one caveat. Plug: my book, Data Push Apps with HTML5 SSE, answers your questions in more detail ;-)
The caveat is that IE still does not support it. My book discusses handling older browsers (it is fairly easy, especially if you plan for it in advance).
In your case, as you only expect new data every 10 minutes, you could just go for long-poll for all browsers: the difference between that and SSE is a micro-optimization.
As another book recommendation on this subject, Ilya Grigorik's High Performance Browser Networking is very good.