Node.js socket.io - client emit not seen on server
NickName:rich remer Ask DateTime:2016-03-26T08:01:15

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.

Copyright Notice:Content Author:「rich remer」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/36229998/node-js-socket-io-client-emit-not-seen-on-server

More about “Node.js socket.io - client emit not seen on server” related questions

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("socke...

Show Detail

express, socket.io server for browser client and node.js client

I am trying to set-up an express server with socket.io that will allow node.js clients and browser clients to connect. The browser connects with no problem. The node.js client using socket.io-clien...

Show Detail

socket.io emit data by android application to node.js server

I have problem with emiting data by android application to node.js server. node.js server: var app = require('http').createServer() var io = require('socket.io')(app); app.listen(1000); app...

Show Detail

How to emit event in socket.io based on client?

I am working on realtime data visualization application using node.js, express and socket.io. Requirement: Have to emit the events based on the client request. For example: If user enter the ur...

Show Detail

Node.js, Socket.io emit not working

I am making a website in Socket.io. But emit method not working in my code. I can't see any errors in my code. Here is my server code. var io = require("socket.io").listen(server); //Some external

Show Detail

socket.io, server emit event before client set up listener for that event

In the following code snippet(using node.js and socket.io library), sometimes (and sometimes not) server emit event something before client side set up socket listener for something event which will

Show Detail

socket.io emit/broadcast to every client

I can not find out how to emit from the client or an other NodeJS file to every client. Emitting to the server worked, but not to every other client. Servercode: var io = require('socket.io').lis...

Show Detail

socket.emit from node.js client to browser client

I am trying to emit a message from the node.js client to the web client. I am assuming this must be done through the server. Any help is appreciated. The erorr I see in the browser is: GET http:/...

Show Detail

socket.io client unable to emit message to server

I can't figure out why my socket.io server isn't receiving messages from the client. Here is my server JS file: var app = require('express')(); var http = require('http').Server(app); var io = req...

Show Detail

Send message to client from server using socket.io and node.js

I'm working with socket.io and node.js . i am able to broadcast messages from server to all clients but i am facing problem while sending the message from server to specific clients. i am new to this

Show Detail