Socket.io - Capture all events

I am working on a Node.js application where almost all communication takes place via sockets.io.

What I need to do, before processing any request (except the login request), make sure that the user is authenticated.

An obvious way to do this would be to have a catch-all listener that gets called before any method executes.

I can not find anything like this in Socket.io. How can i achieve Is there a better approach than the one I take?

+4
source share
2 answers

The best approach here is to authenticate the user when connecting (shaking hands), parsing cookies and decoding the session.

Read more about this in the following links:

http://www.danielbaulig.de/socket-ioexpress/ (it contains a detailed guide on everything you need to do)
https://github.com/LearnBoost/socket.io/wiki/Authorizing
socket.io and session?

+1
source

Exit socket.io-events in the nom for "Catch all events"

 var router = requrie('socket.io-events')(); router.on('*', function (sock, args, cb) { //do something with the args! cb(); }); var io = require('socket.io')(3000); 

Quit socket.io-handshake in nom to support socket.io v1.x

0
source

Source: https://habr.com/ru/post/1388379/


All Articles