Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.io client not receiving messages from server

I'm trying to implement a system with two clients one of them sends a message and the other one shall receive it. The figure below will explain it in a more visual way:

socket.io message communication chart

So, the client 1 send the message to the server (and this works), the server receives a "push" message and emits a "pop" message that should be picked up by Client 2. The problem here is that Client 2 never receives the "pop" message. :(

Here's the code for all of them.

SERVER.JS

var app = require('express').createServer()   , io = require('socket.io').listen(app);  app.listen(999);  app.get('/webclient', function (req, res) {     res.sendfile(__dirname + '/web.html'); });  app.get('/mobile', function (req, res) {     res.sendfile(__dirname + '/mobile.html'); });  io.sockets.on('connection', function (socket) { //      socket.emit('pop', { hello: 'world' });     socket.on('push', function (data) {         console.log('push received, emitting a pop');         socket.emit('pop', { hello: 'world' });     }); }); 

CLIENT 1 ( aka mobile.html )

<html>     <head>         <title>             Mobile         </title>         <script src="/socket.io/socket.io.js"></script>         <script src="https://ajax.googleapis.com/ajax/libs/mootools/1.4.5/mootools-yui-compressed.js" type="text/javascript"></script>         <script>           var socket = io.connect('http://localhost:999');         </script>     </head>     <body>         <input type="button" name="act" id="push" value="message" />         <script type="text/javascript" charset="utf-8">             window.addEvent('domready', function() {                 $('push').addEvent('click', function() {                      socket.emit('push', { hello: 'world' });                 });             });         </script>     </body> </html> 

CLIENT 2 (aka web.html)

<script src  = "/socket.io/socket.io.js"></script> <script>   var socket = io.connect('http://localhost:999');   socket.on('pop', function (data) {     console.log(data);   }); </script> 

I just cannot understand the reason why Client 2 does not receive the "pop" message, I'm quite new to socket.io and node.js in general so some mechanics to me are still a bit obscure, so I apologize in advance for my noobness. :)

cheers

-k-

like image 781
holographix Avatar asked Mar 23 '12 10:03

holographix


People also ask

Why is Socket.IO not working?

First and foremost, please note that disconnections are common and expected, even on a stable Internet connection: anything between the user and the Socket.IO server may encounter a temporary failure or be restarted. the server itself may be killed as part of an autoscaling policy.

How do I connect to a Socket.IO server?

listen(port); // Create a Socket.IO instance, passing it our server var socket = io. listen(server); // Add a connect listener socket. on('connection', function(client){ console. log('Connection to client established'); // Success!

Does Socket.IO need a server?

Socket.io, and WebSockets in general, require an http server for the initial upgrade handshake. So even if you don't supply Socket.io with an http server it will create one for you.

What is the difference between Socket.IO and socket IO client?

socket-io. client is the code for the client-side implementation of socket.io. That code may be used either by a browser client or by a server process that is initiating a socket.io connection to some other server (thus playing the client-side role in a socket.io connection).


1 Answers

The function passed to .on is called for each socket to do the initialization (binding events etc), and socket refers to that current socket. This will be client 1 when you receive a push message, because the handler function is bound to the push event of that socket - you bound that function when client 1 connected (where socket refers to client 1).

io.sockets refers to all sockets connected, so including client 2 in your case.

like image 65
pimvdb Avatar answered Oct 03 '22 07:10

pimvdb