npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

socket.io-monitor

v2.0.1

Published

Monitor sockets, rooms, events in your socket.io server. This module includes the server and client libraries. Edit

Downloads

188

Readme

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)

  1. Bind your socket.io server:
const monitor = require('socket.io-monitor')
const { emitter } = monitor.bind(io, { server: false })
  1. In same process, use emitter to grab info:
emitter.getState()
/* {
  rooms: [ { name: 'room1', sockets: [ 'id1', 'id2' ] }, … ],
  sockets: [ { id: 'id1', connectedAt: 12345 }, { id: 'id2', connectedAt: 56789 }, … ]
} */

emitter.on('join', ({ id, rooms }) => console.log('socket %s joins rooms %s', id, rooms))

Usage (remote)

  1. Bind your socket.io server:
const monitor = require('socket.io-monitor')
const { server } = monitor.bind(io, { port: 9042, host: 'localhost' })

server.then(srv => {
  console.log('connection OK')
})
  1. In another process, connect to your monitor server:
const monitor = require('socket.io-monitor')
const client = monitor.connect({ port: 9042, host: 'localhost' })

client.then(emitter => {
  console.log('connection OK')
  emitter.on('join', ({ id, rooms }) => console.log('socket %s joins rooms %s', id, rooms))
})

Events

  • (remote only) init state
    • (local only) method getState() returns state
  • (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 representation
const { emitter } = monitor.bind(io, options)

io.on('connection', socket => {
  socket.on('login', username => {
    // store somewhere that socket is bound to this username
    emitter.emit('string', { id: socket.id, string: username })
  })
})

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.bind(io, options)
// 'join' event is emitted each time a socket joins a room
emitter.on('join', ({ id, room }) => {
  // we only have to check if room has the known prefix, and voilà!
  if (room.match(/^user:/)) {
    emitter.emit('string', { id, string: room.substring(5) })
  }
})

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.on('client', (client, state) => {
  // state.rooms = list of rooms with socket ids in them
  // state.sockets = list of sockets
  // in our sample, an identified socket is in a room named "user:$username"
  state.rooms.forEach(({ name, sockets }) => {
    if (name.match(/^user:/)) {
      const id = sockets[0]
      const string = name.substring(5)
      client.emit('string', { id, string })
    }
  })
})