I created a chat application with android java as client and node.js as server side.
Client side:
IO.Options opt=new IO.Options();
opt.query="token=anything";
socket = IO.socket("http://xxx.xx.xxx.xxx:PORT",opt);
socket.connect();
i want to send a token on connection and get it in server for validation... how can i get the token that i pushed in opt.query in node.js?
And if you have good idea for sending some data like tokens on connection or somework for secure the connection Please note that.
Thank you
Solution server side:
io.on('connection',function(socket){
console.log(socket.handshake.query.token);
});
thanks for @Marcos
You can access the query options in Node.js via socket.handshake.query
const ws = io(http);
ws.use(async(socket, next) => {
try {
await validateToken(socket.handshake.query.token));
return next();
} catch(e) {
return next(new Error('Authentication error'));
}
});
ws.on('connection', socket => {
/* ... */
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With