-
Notifications
You must be signed in to change notification settings - Fork 4
Description
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:
-
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
. -
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 /
:
socket.io-binding/src/index.ts
Lines 98 to 101 in 87a9973
/** | |
* @todo Support custom namespaces. | |
*/ | |
nsp: '/', |
The minimum set of functionality that should be supported is:
-
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. -
The
emit()
andsend()
functions need to take a namespace parameter. The same issue with variadic arguments applies. -
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.)socket.io-binding/src/index.ts
Line 149 in 87a9973
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
or44
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.