socket.io-monitor
Monitor sockets, rooms, events in your socket.io server. This module includes the server and client libraries.
See socket.io-monitor-cli for a realtime dashboard in your console.
Installation
npm install --save socket.io-monitor
Usage (local)
- Bind your socket.io server:
const monitor = const emitter = monitor
- In same process, use emitter to grab info:
emitter/* { rooms: [ { name: 'room1', sockets: [ 'id1', 'id2' ] }, … ], sockets: [ { id: 'id1', connectedAt: 12345 }, { id: 'id2', connectedAt: 56789 }, … ]} */ emitter
Usage (remote)
- Bind your socket.io server:
const monitor = const server = monitor server
- In another process, connect to your monitor server:
const monitor = const client = monitor client
Events
- (remote only) init
state
- (local only) method getState() returns
state
- (local only) method getState() returns
- (local only) client
{ client, state }
client
is the remote monitor client (you can listen/emit to all remote events)state
is the initial state data, sent along init event
- broadcast
{ name, args, rooms, flags }
- join
{ id, rooms }
- leave
{ id, room }
- leaveAll
{ id }
- connect
{ id }
- disconnect
{ id }
- emit
{ id, name, args }
- recv
{ id, name, args }
- string
{ id, string }
- this event should be used by monitor client implementation to display alternative string representation of a socket. This event is never emitted for you, see example below.
State
- rooms:
[ { name: string, sockets: [ string ] } ]
- sockets:
[ { id: string, connectedAt: timestamp-ms } ]
Socket string representation
You can emit string
event to provide alternative string representation for a socket, that can be used by monitor client.
// Example 1: when user emits a "login" event, we use it as string representationconst emitter = monitor io
Real-life use case: once a socket is authenticated and bound to a user we put it in a dedicated room user:$username. This is frequently done to be able to target a socket knowing only the username. We take advantage of this situation to only rely on emitter instead of modifying existing code:
const emitter = monitor// 'join' event is emitted each time a socket joins a roomemitter
In both cases however, emitting string representation just when a socket connects is not enough: when you connect a monitor client, it will fetch existing sockets and will not receive string events for them. In current version it's up to you to handle this case too. This part may change in the future to make it easier. The current best way is to listen for client event which is called when a monitor client receives state data:
emitter