Im using the following code in index.js
io.on('connection', function(socket){
console.log('a user connected');
console.log(socket.id);
});
the above code lets me print the socket.id in console.
But when i try to print the socket.id on client side using the following code
<script>
var socket = io();
var id = socket.io.engine.id;
document.write(id);
</script>
it gives 'null' as output in the browser.
You should wait for the event
connect
before accessing theid
field:With this parameter, you will access the sessionID
Edit with:
Client-side:
Server-side:
For Socket 2.0.4 users
Client Side
Server Side
To get client side socket id for Latest socket.io 2.0 use the code below
The following code gives socket.id on client side.