Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple socket.io room and auth example
const io = require('socket.io-client');
const socket = io('http://localhost:3000', {
transportOptions: {
polling: {
extraHeaders: {
'Authorization': 'Bearer abc',
},
},
},
});
socket.on('connect', () => {
console.log('connected!');
socket.emit('room', 'room1');
});
socket.on('message', data => {
console.log(data);
});
const server = require('http').createServer();
const io = require('socket.io')(server);
const isValidJwt = (header) => {
const token = header.split(' ')[1];
if (token === 'abc') {
return true;
} else {
return false;
}
};
// io.of('/test');
io.use((socket, next) => {
const header = socket.handshake.headers['authorization'];
console.log(header);
if (isValidJwt(header)) {
return next();
}
return next(new Error('authentication error'));
});
io.on('connection', (socket) => {
socket.on('room', room => {
console.log(room);
socket.join(room);
});
});
setInterval(() => {
io.sockets.to('room1').emit('message', 'what is going on, party people?');
}, 3000);
setInterval(() => {
io.sockets.to('room2').emit('message', 'anyone in this room yet?');
}, 3000);
server.listen(3000);
@nitko12

This comment has been minimized.

Copy link

@nitko12 nitko12 commented Mar 15, 2020

Thank you, exactly what I needed!

@andrevbnk

This comment has been minimized.

Copy link

@andrevbnk andrevbnk commented Jan 18, 2021

<3 thx

@ardaorkin

This comment has been minimized.

Copy link

@ardaorkin ardaorkin commented Apr 9, 2021

Thank you! It is very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment