What is SSE
SSE (Server Sent Events) is one of the methods that the server can push update to client without client sending any request, another method is web socket (ws). Push based update approach is much more efficient than poll based approach because in poll based approach, the client keep sending requests to the server to fetch the latest update, but most probably there is no update from the server, so the requests are likely to be a waste of bandwidth and computation power; in contrast in push based methods the server will push updates to clients only when there is an update.
Each event will start with data:
and end with two line break characters \n\n
SSE vs WS
SSE is one way, the client requests once and open the connection, then server can keep pushing events ever the same connection, but client cannot push any more data using the same connection; ws is two way, the client requests once and open the connection, then both client and server can use the same connection to push updates to each other.
How client start an SSE and server handle it
How I test an SSE endpoint
Here i would like to test the above SSE endpoint using jest. looked into the popular supertest library, but seems that doesnt support SSE. Some may say mock everything but we do not want to mock this kind of network traffic in acceptance or e2e or integration test.
So here we really start an express server inside the test case and do real network requests in our test cases. One thing to note is that SSE keeps the connection live(unless we stop it specifically but some cases we may never stop it), so I used stoppable to wrap the express server. Hence when i stop the express server in the afterAll hook, it will stop all the on going SSE connections for me and prevent the test process never stop.