rich remer
rich remer

Reputation: 3577

Node.js socket.io - client emit not seen on server

I'm trying to put together a simple Socket.IO test case in Node.js, but I'm not seeing the events emitted from the client getting to the server.

var http = require("http"),
    io = require("socket.io"),
    io_client = require("socket.io-client"),
    listener, port, server, url,
    socketServer, socketClient,
    i = 0;

port = process.env.LISTEN_PORT || 80;
url = "http://localhost:" + port;
server = http.createServer();
socketServer = io(server);
socketClient = io_client(url);

socketServer.on("connection", function(socket) {
    console.log("server:connection");
    socket.emit("ping", ++i);
    socket.on("pong", function(data) {
        console.log("server:pong", data);
    });
});

socketClient.on("ping", function(data) {
    console.log("client:ping", data);
    socketClient.emit("pong", data);
});

listener = server.listen(port, function() {
    console.log("Listening on port " + listener.address().port);
});

expected output

Listening on port 80
server:connection
client:ping 1
server:pong 1

actual output

Listening on port 80
server:connection
client:ping 1

The connection is made, data is moving from server to client, but I don't see the corresponding events emitted on the server when the client triggers a socket event.

Upvotes: 0

Views: 787

Answers (1)

jfriend00
jfriend00

Reputation: 708026

I believe that ping and pong are reserved message names in socket.io. I'd suggest you change those message names to something else. socket.io has a few reserved message names that are not well documented and it probably should endeavor to use less likely names, but since all the clients are already out there, they are probably stuck with what they have.

Here's a similar question that was fixed by changing the message names.

Also, in case there's something wrong with your client sending code, please include that relevant code in your question.

Upvotes: 2

Related Questions