socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Messages not being sent from client

Open iamdriz opened this issue 11 years ago • 6 comments

I have three Node applications that need to talk to each other that contain the following code:

APP 1

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

io.on('connection', function (socket) {
socket.on('comms', function (data) {
    console.log(data);
});
});

io.listen(8004);

setInterval(function(){
    io.emit('comms', {'hello':'world'});
}, 2000);

APP 2 (listens for comms from APP 1 and 3)

var socket = require('socket.io/node_modules/socket.io-client')('http://localhost:8004');

socket.on('comms', function (data) {
    console.log(data);
});

APP 3

var socket = require('socket.io/node_modules/socket.io-client')('http://localhost:8004');

setInterval(function(){
    socket.emit('comms', {'goodbye':'world'});
}, 2000);

However I only get the comms from APP 1 and not from APP 3 appearing in APP 2.

I do see the comms from APP 3 in APP 1 but not APP 2.

What's the issue? Do I then have to send comms back down from 1 that came from 3 to 2?

iamdriz avatar Jul 29 '15 12:07 iamdriz

There is a space between socket and .emit in app 3 On 29 Jul 2015 17:43, "iamdriz" [email protected] wrote:

I have three Node applications that need to talk to each other that contain the following code:

APP 1

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

io.on('connection', function (socket) {

});

io.listen(8004);

io.emit('comms', {'hello':'world'});

APP 2

var socket = require('socket.io/node_modules/socket.io-client')('http://localhost:8004');

socket.on('comms', function (data) { console.log(data); });

APP 3

var socket = require('socket.io/node_modules/socket.io-client')('http://localhost:8004');

socket .emit('comms', {'goodbye':'world'});

However I only get the comms from APP 1 and not from APP 3. So the emitting of comms from an IO instance works fine, but when trying to send it up from the client it doesn't work...

APP 1 is launched before APP 2 and 3 so the IO server does exist at this point.

What's the issue?

— Reply to this email directly or view it on GitHub https://github.com/socketio/socket.io/issues/2188.

maxzeroedge avatar Jul 29 '15 12:07 maxzeroedge

@maxzeroedge That was just a typo I made here.

iamdriz avatar Jul 29 '15 12:07 iamdriz

Try io.sockets.emit or socket.broadcast.emit in app 1. io.emit will only emit for the current connection, which may be only to app 3 when app 3 is sending. On 29 Jul 2015 18:29, "iamdriz" [email protected] wrote:

@maxzeroedge https://github.com/maxzeroedge That was just a typo I made here.

— Reply to this email directly or view it on GitHub https://github.com/socketio/socket.io/issues/2188#issuecomment-125941539 .

maxzeroedge avatar Jul 29 '15 13:07 maxzeroedge

@maxzeroedge APP 1 emits fine. It's APP 3 that's not emitting!

iamdriz avatar Jul 29 '15 13:07 iamdriz

You realize that the app 3 emits to app 1 (if I am correct in assuming it is the socket.io server) which emits or rather relays back to app 3 and not to app 2. In websockets, the server has to act like a relay to transmit data. On 29 Jul 2015 18:40, "iamdriz" [email protected] wrote:

@maxzeroedge https://github.com/maxzeroedge APP 1 emits fine. It's APP 3 that's not emitting!

— Reply to this email directly or view it on GitHub https://github.com/socketio/socket.io/issues/2188#issuecomment-125944666 .

maxzeroedge avatar Jul 29 '15 13:07 maxzeroedge

That issue was closed automatically. Please check if your issue is fixed with the latest release, and reopen if needed (with a fiddle reproducing the issue if possible).

darrachequesne avatar Jan 12 '17 15:01 darrachequesne