Sending message to specific client with socket.io and empty message queue


Question

I´m going crazy with socket.io! Documentation is so bad it's simply not true.

I want to send a feedback to specific client over socket.io

My server side looks like this:

app.get('/upload', requiresLogin, function(request, response) {
    response.render('upload/index.jade');
    io.sockets.on('connection', function (socket) {
        console.log('SOCKET ID ' + socket.id);
        io.sockets.socket(socket.id).emit('new', 'hello');
    });
});

and the client side looks like this:

$(document).ready(function() {
    var socket = io.connect('http://localhost:80/socket.io/socket.io.js');
    socket.on('new', function (data) { 
        console.log(socket.id);
        console.log(data); 
        //$('#state').html(data.status);
    });
});

but the client does simply nothing. I have tried nearly everything. Can someone tell me what I am doing wrong, please! :(

1
36
8/14/2014 8:33:12 PM

to send a message to a specific client save every one that connects to the server in an Object.

var socketio = require('socket.io');
var clients = {};
var io = socketio.listen(app);

io.sockets.on('connection', function (socket) {
  clients[socket.id] = socket;
});

then you can later do something like this:

var socket = clients[sId];
socket.emit('show', {});
52
8/2/2011 3:29:45 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon