Search code examples
javascriptwebsocketsocket.ioserver-sent-events

SSE or WebSockets for Stackoverflow-like instant notification


What is the best technology to use if I want to make instant user notification, like StackOverflow has?

I consider SSE and WebSockets. What are pros and cons of each solution?

Should I use socket.io or better to use WebSockets directly?


Solution

  • The main difference is that with SSE you can only receive messages from the server. You cannot send messages to the server. Everything doable with SSE is doable with WebSockets. But not vice versa - WebSocket is capable of sending data to the server. So from that point of view WebSockets win. I can't really see any advantage of SSE (perhaps performance?), but then again I don't have much experience with it.

    Note that StackOverflow uses WebSockets. They might have some fallback for older browser, I don't know about that.

    As for the third question: perhaps you should ask what language you want to use in the first place? I've been working with WebSockets and Python and it worked really well. You could work with WebSockets directly. The advantage of using socket.io is mostly the fallback (assuming it matters - it does not IMHO): if WebSockets are not available it can automatically switch to other ways of communication (like Flash or long polling). The disadvantage is that it is Node.js (in the sense that you have to restrict yourself to one language) plus there are some performance issues, i.e. socket.io does not scale well beyond one machine.