How to emit and receive without authentication using socketio-jwt

My sockets work fine when the client has a token (provided by Laravel jwt), but I need to work with clients while they are not yet authenticated. I need something like:

io.sockets
.on('connection', socketioJwt.authorize({
  secret: process.env.JWT_SECRET,
  timeout: 15000 // 15 seconds to send the authentication message
}))

.on('authenticated', function(socket) {
  console.log('Authenticated!!');
  // This works:
  socket.on('setDataRegistered', function(datos) {
    console.log('Registering!');
  });
})
// This doesn't works:
.on('config', function(socket) {
  console.log('A client wants to be configured before authenticated!');
})

How can I call from FrontEnd to 'config' (socket.emit ('config')) before authentication?

Thank you for your help. Sorry, my English.

+4
source share
1 answer

What am I doing:

io.on('connection', function (socket) {
        // Client connected but not authenticated. Client has to emit to 'authenticate' for authentication

        socket.on('authenticate', function (data, fn) {
            // Authenticate socket.

            // define more events
        });

        // still accessible to unauthorised sockets
        socket.on("other-event", function(data) {

        });
});

I do not use middleware for authentication. But it's just me. I never found for this use.

+1
source

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


All Articles