Skip to content Skip to sidebar Skip to footer

How To Get Specific Cookie Value In Socket.io

I can now display the all cookie using socket.request.headers.cookie. When I console the output is like this PHPSESSID=mtklg8k81cpkop5ug6aechbb34; user=77; io=1Klg6xgTRXhb2OWiAAAA

Solution 1:

You have to parse the cookies into their separate cookies and properties. The usual module to use for that is cookie-parser.

For use with socket.io, you can use a small wrapper with it for use with socket.io called socket.io-cookie-parser.

const cookieParser = require('socket.io-cookie-parser');
io.use(cookieParser());

io.on('connection', function(socket) {
    // access socket.request.cookiesconsole.log(socket.request.cookies['user'];
});

Post a Comment for "How To Get Specific Cookie Value In Socket.io"