Monthly Archives: مهر 1392

Answer by Nasser for socket io mouse click event

client side:

in client side, you should first define an event listener for every type of event you need (using jQuery). in that listener, simply emit an event containing ID of element that triggered the event. so server can broadcast that to all other clients.

also, if an event received from server, you should simulate that on it's corresponding element via jQuery.

$(document).on('click', function(event){
    socket.emit('myClick', {id:});

var socket = io.connect('http://localhost');

socket.on('myClick', function (data) {

server side:

in server side, just emit whatever event that was triggered, to all other clients except sender.

var io = require('').listen(80);

io.sockets.on('connection', function (socket) {
    socket.on('myClick', function (data) {
        socket.broadcast.emit('myClick', data);

Comment by Nasser Torabzade on How to access cookie data inside a connection?

I think so! I have two separated functions in my app, one for serving files like this: http.createServer(onRequest).listen(httpPort); function onRequest (request, response) { // stuff for serving files } and another function for serving io.sockets.on('connection', function (socket) { // stuff for handling events } and these two are called in my index.js , Do you think I need to change this architecture?

How to access cookie data inside a connection?

I am using node.js and on the client side, I wrote a cookie like this:

var socket = io.connect('http://localhost:3000');
socket.emit('this', { is: 'test'});

and on the server side, I need to be able to read from that cookie inside a connection, something like this:

io.sockets.on('connection', function (socket) {
    socket.on('this', function(reqData){ 
        console.log(socket.handshake.headers); // there is no cookies here!

a no-framework solution is preferred, any help is appreciated. thanks.

update: in this gist you can find my complete code.