# Socket.IO

## Features of this Example

* Socket.IO connect and disconnect
* Emit events to and from client

### Walkthrough

This example was referenced in a walkthrough about [Socket.IO Health Checks and Uptime](https://asserted.io/posts/socketio-health-check-uptime).

### Try it out

Repo is available [here](https://github.com/assertedio/socketio-uptime).

```bash
# Clone example
git clone https://github.com/assertedio/graphql-uptime

# Enter directory and install
cd node-uptime/
npm install

# Run asserted tests
npm run test:asrtd
```

### Tests

Can also be viewed on github [here](https://github.com/assertedio/graphql-uptime/blob/master/.asserted/example.asrtd.js).&#x20;

```javascript
const { expect } = require('chai');
const io = require('socket.io-client');
const util = require('util');
const sinon = require('sinon');

const sleep = util.promisify(setTimeout);

const monitoringClient = io('http://localhost:3000', { forceNew: true });

describe('socketio api tests', () => {
  let client;

  before((done) => {
    monitoringClient.emit('add user', 'monitoring');
    monitoringClient.once('connect', () => done());
  });

  beforeEach((done) => {
    client = io('http://localhost:3000', { forceNew: true });
    client.once('connect', () => done());
  });

  afterEach(() => {
    client.disconnect();
  });

  after(() => {
    monitoringClient.disconnect();
  });

  it('user joined and login', async () => {
    const login = sinon.stub();
    const joined = sinon.stub();

    monitoringClient.once('user joined', joined);
    client.once('login', login);

    client.emit('add user', 'new-user');

    // Admittedly a bit gross to use sleep here, but just wanted something simple.
    // The less brittle approach would be to block on a promise until the stubs are called.
    await sleep(100);

    expect(login.args).to.eql([[{ numUsers: 2 }]]);
    expect(joined.args).to.eql([[{ numUsers: 2, username: 'new-user' }]]);
  });

  it('user sent message', async () => {
    const monitoringMessage = sinon.stub();
    const clientMessage = sinon.stub();

    monitoringClient.once('new message', monitoringMessage);
    client.once('new message', clientMessage);

    client.emit('add user', 'new-user');
    await sleep(100);
    client.emit('new message', 'some-message');
    await sleep(100);

    expect(monitoringMessage.args).to.eql([[{ message: 'some-message', username: 'new-user' }]]);
    expect(clientMessage.args).to.eql([]);
  });
});
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.asserted.io/examples/socket.io.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
