Monitoring Socket.IO Uptime
Tags: node, devops, testing, javascript
Published: 2020-07-08 12:55:51

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](https://github.com/assertedio/socketio-uptime)

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](https://github.com/socketio/socket.io/tree/master/examples/chat). 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.

{% gist https://gist.github.com/ehacke/fc7e51f14ff8d284c975cbb41be90e89.js %}

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.

{% gist https://gist.github.com/ehacke/57a08e122c334220c9a249e8de48bf03.js %}

## Routine Configuration

The routine.json is slightly different this time, only in that it makes use of [custom dependencies](https://docs.asserted.io/reference/included-dependencies#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 socket.io-client, we can prune out all the other dependencies we don't need.

{% gist https://gist.github.com/ehacke/c2636a195aec1f8726c61d7c4458ccff.js %}

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

{% gist https://gist.github.com/ehacke/1994fedc0d3f199247293172fbb394d2.js %}

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.

{% gist https://gist.github.com/ehacke/fe7c7d522a0b9e551f6a9a1e2e257f1c.js %}

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](https://github.com/assertedio/socketio-uptime) 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](https://asserted.io). 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](https://docs.asserted.io/).
3. Start writing and running tests in prod!