Monthly Archives: مهر 1392

Answer by Nasser Torabzade 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 socket.io 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: event.target});
}

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

socket.on('myClick', function (data) {
    $(data.id).trigger('click');
}

Server side:

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

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

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

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 socket.io 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: event.target});
}

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

socket.on('myClick', function (data) {
    $(data.id).trigger('click');
}

server side:

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

var io = require('socket.io').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 socket.io 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 socket.io: 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 socket.io connection?

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

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

and on the server side, I need to be able to read from that cookie inside a socket.io 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.