Skip to content

Support for namespaces #17

@rgov

Description

@rgov

An important part of Socket.IO is that events can be sent to different namespaces. For example, consider a home automation app that emits a power_on event to one of several namespaces representing a smart device, /living_room/lamp, /bedroom/ceiling_fan, etc. (It should be noted that under the hood, Engine.IO multiplexes distinct namespaced sockets over one WebSocket transport.)

With socket.io-binding, it is currently not possible to:

  1. Capture events and inspect their namespaces. For example, writing a test that expects that clicking a button specifically sends power_on to /bedroom/ceiling_fan.

  2. Inject events into a particular namespace. For example, injecting a response to the power_on event on the same namespace to ensure that the application state is updated accordingly.

Specifically, (1) is not possible because decodedSocketIoPacket.nsp is not passed to the event callback. And (2) is not possible because the outgoing packet is always constructed with the default namespace /:

/**
* @todo Support custom namespaces.
*/
nsp: '/',

The minimum set of functionality that should be supported is:

  1. The event callback should be extended to pass the namespace. This could possibly be done by extending the MessageEvent interface with a .socketio.namespace field, or by adding an additional argument. Because the function is variadic, adding an additional argument would be a breaking change.

  2. The emit() and send() functions need to take a namespace parameter. The same issue with variadic arguments applies.

  3. The mock needs to be expose a way to accept and reject requests to join a namespace. Currently, the mock always sends the client 40 that joining the root namespace succeeded. (See the protocol reference.)

    this.rawClient.send('40' + JSON.stringify({ sid: 'test' }))

    Instead, there should be a callback that accepts at least a namespace string and can return a boolean of whether to allow the connection. The response prefix should be 40 or 44 depending on success or failure, followed optionally by /nsp, for the non-root namespace /nsp.

Finally, it's worth emphasizing that this is a protocol-level feature, unlike, say, rooms, so it is necessary for being able to properly intercept and inject Socket.IO messages.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions