Skip to content Skip to sidebar Skip to footer

Node.js, Socket.io: How To Get Client Browser-language?

I am trying to get the language the user uses in order to serve the right sound files for a playing video, using socket.io and node.js. I am a total beginner with node.js and socke

Solution 1:

You're almost there. Do this:

var socket = io.connect('http://localhost:1337', { lang: language });

Instead of {query: language} because query is a reserved object.

And you can access this by doing this:

io.set('authorization', function(handshakeData, cb) {
console.log(handshakeData.query.lang);
cb(null, true);});

You can also access it like this:

io.sockets.on('connection', function(socket){
  console.log(socket.handshake.query.lang);
});

If you don't want to append language variable to query then you can access the language by this:

io.sockets.on('connection', function(socket){
  console.log(socket.handshake.headers['accept-language']);
});

Post a Comment for "Node.js, Socket.io: How To Get Client Browser-language?"