How can I get the hostname of a socket?

node.js, socket.io

Solution

try this:

console.log(socket.handshake.headers.host);

split port if necessary:

console.log(socket.handshake.headers.host.split(":").shift());

Problem

When I receive a certain event from a connected socket, I have to send a request with as parameter my hostname and port. I was hoping to be able to retrieve this information from the socket object. Unfortunately, there is little documentation on this and I can't seem to be able to find out if and how this is possible. So, is it possible to do something like this in Socket.io: ``` io.sockets.on('connection', function(socket){ console.log(socket.manager.server.hostname) })' ``` (Or, alternatively: which thinking error am I making here in thinking that this should be possible in the first case?)

Original source