Performance Test: WebSockets vs. Server Sent Events (EventSource)
Server Sent Events (SSE) and WebSockets are similar technologies on the surface. The major differences seem to be that SSE does not require a new protocol or full duplex connection. Instead, vanilla HTTP is used. And of course, SSE sends messages only from server to client and not from client to server.
Server Sent Events are supported by ~60% of browsers, but not any version of IE. WebSockets are supported by ~55% of browsers, including IE10.
I wanted to informally test the speed of each technology. In this experiment, WebSockets and Socket.io were pitted against EventSource and connect-sse in Safari 6.0.2 on a MacBook Pro 2.4GHz Core i5. An iteration of the test has the server send 10,000 messages and records the time to send all 10,000 from the server, and the time to receive all 10,000 on the client. 100 Iterations were performed. The results were then averaged and the standard deviation calculated.
Check out the source code for testing and reporting on GitHub.
Initially it looked like Socket.io was 2-4 times slower than SSE. Then, I remembered Socket.io has debug mode enabled by default (doh). After correcting this oversight, the speed of each was shown to be within the same order of magnitude.
The mean time to send 10,000 messages averaged over 100 iterations (µ) is recorded in milliseconds.
fig. 1 Results for sending SSE events to the client fig. 2 Results for sending Socket.io events to the client fig. 3 Results for receiving SSE events from server fig.4 Results for receiving Socket.io events from server