Linked Questions

Popular Questions

ID from JWT token to id client in socket.io?

Asked by At

After logging in to my application, the socket.io is started and jwt token is generated. In my token is userId.

I would like to assign this id to user id socket.io and store them in redis, because at some point I send a socket and I would like to send it to the client only with the id from the token.

const socket = socketIOClient(this.state.endpoint);
socket.broadcast.to(this.state.recipientId).emit('new notification');

// this.state.recipientId <- this is id from token JWT

so how do I decode the server-side token jwt and extract the id from it, then assign it to socket.id?

this is my server.js socket.io fragment code:

io.on('connection', socket => {
  socket.on('new notification', () => {
    io.sockets.emit('new notification');
  });

  socket.on('disconnect', () => {
    io.emit('user disconnected');
  });
});

Related Questions