Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to emit and recieve whitout a Authenticated using socketio-jwt

My Sockets work fine when a client has the token (provided by Laravel jwt) but, a need to work with the clients while they aren't authenticated yet, I Want 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 the FrontEnd to 'config' (socket.emit('config')) before authenticate??

Thanks for your help. Sorry my English.

like image 385
bluesky777 Avatar asked Oct 20 '16 13:10

bluesky777


1 Answers

What I do is this:

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 don't use any middleware for authentication. But that's just me. I never found the use for it.

like image 189
Abhyudit Jain Avatar answered Sep 28 '22 00:09

Abhyudit Jain