0

I'm in a use case to track all the socket.io notification transactions, which are triggered by the server , similar API req/res counter to validate subscription validation..

We're using a express middleware , which tracks all the REST API requests made by the users and levy subscriptions rules accordingly.Now trying implement similar kind of middle ware on socket io notification also ..

Technology Stack : NodeJS , Loopback , Express , Socket IO.

Any suggestions please ..

  • 1
    You can track `.emit()` by overriding it on both `io.emit()` and `socket.prototype.emit()`. There is no built-in way to monitor all incoming events. There are some patches out there that allow you to do it. You can also override `socket.prototype.on()` and capture all events that are being listened to. – jfriend00 Dec 08 '16 at 05:14
  • Thank you for the reply ... Could you please elaborate you answer , basically my intention was to define a handler through which all the socket events should pass .. – archi_geek Dec 08 '16 at 10:09
  • socket.io does not work that way by itself. You can try one of these: https://github.com/lmjabreu/socket.io-wildcard or https://github.com/hden/socketio-wildcard.or https://github.com/fullstackers/socket.io-events to see if they add what you want. – jfriend00 Dec 08 '16 at 14:42
  • Also, similar discussion here: [Socket.io Client: respond to all events with one handler?](http://stackoverflow.com/questions/10405070/socket-io-client-respond-to-all-events-with-one-handler) and long discussion on the socket.io Github repository: [Add wildcard support for events](https://github.com/socketio/socket.io/issues/434) with multiple solutions mentioned. – jfriend00 Dec 08 '16 at 14:43

1 Answers1

0

You could use socket.use()

From the docs:

Registers a middleware, which is a function that gets executed for every incoming Packet and receives as parameter the packet and a function to optionally defer execution to the next registered middleware.

Errors passed to middleware callbacks are sent as special error packets to clients.

io.on('connection', (socket) => {
  socket.use((packet, next) => {
    if (packet.doge === true) return next();
    next(new Error('Not a doge error'));
  });
});
Krimson
  • 7,386
  • 11
  • 60
  • 97