Example on GitHub
Socket.IO is a library that leverages websockets and standard HTTP to enable real-time, bi-directional communication. Depending on your use case, Socket.IO is often faster to implement and less error-prone than raw websockets as it supports things like broadcast and protocol fallback out of the box.
The example I'm going to work with is a modified version of the demo provided here. It's an extremely simple example of a chat app using Socket.IO.
The server that the Asserted tests will run against contains two primary files.
The first is the Socket.IO logic that handles new connections and responds to messages emitted from the client.
This allows users to join and disconnect, as well as broadcast messages to other users.
The second file is where the Socket.IO logic is connected to the server.
The routine.json is slightly different this time, only in that it makes use of custom dependencies. Custom dependencies are available on paid plans, and here we're using that option to include the Socket.IO client library in our tests.
The package.json for the routine (inside the .asserted directory) is slightly different than the default in this case because of the custom dependencies. On top of adding socket.io-client, we can prune out all the other dependencies we don't need.
Continuous Integration Tests
We created two different clients in this case. One to act as a new user joining the chat and sending a message, and the other client to observe the new user joining and the message.
The new user client is recreated for every test case.
The before and after hooks ensure that things are cleaned up properly, which is important if this is running continuously in production or staging.
The tests themselves check that the appropriate events are emitted to the appropriate clients when the new user joins, and when they send a message.
With tests similar to these you can continuously monitor your SocketIO APIs in production and track uptime accurately.
While the example shown here can be cloned and run locally without an account, you'll need to do a few extra steps if you want to create your own Asserted routine to integration test your API in production.