Socket.IO Health Checks and Uptime

Monitoring the health and availability of Socket.IO APIs can be complex. With Asserted you can write sophisticated uptime tests using the Socket.IO client library.
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.

Example Server

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.

Routine Configuration

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.

Routine package.json

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, 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.

Next Steps

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.

  1. Create an Asserted account. It's free and easy.
  2. Complete the 2 minute onboarding to ensure that your environment is ready. You can also reference the docs here.
  3. Start writing and running tests in prod!