4

I have an app like this following:

io.of('/hello').on('connection', function(socket) {
    socket.emit('world', {});
});

app.post('/', function *(next) {
    console.log("At here......");
    var pushMessage = (yield parse.json(this));
    console.log(pushMessage);
    if(flag !== 0) {
//        io.of('/hello/').emit('world', pushMessage);
        io.sockets.emit('world', pushMessage);
    } else {
        console.log("Do Nothing");
    }
});

It receive a http request and emit an event. When I use io.sockets.emit it works well but when I specify a namespace with 'io.of('hello').emit' it doesn't work,why?

My client side is this:

var socket = io.connect('http://localhost:3000', {
  'reconnection delay': 100,
  'reconnection limit': 100,
  'max reconnection attempts': 10
});
//server side use io.sockets.emit
socket.on('world', function(data) {
  alert(data.a);
});

//if server side use io.of('/hello/').emit
//socket.of('/hello/').on('world', function(data) {
//  alert(data.a);
//});
AriesDevil
  • 277
  • 4
  • 7
  • 19

1 Answers1

13

Your code is more or less fine, but you are on different namespaces.

io.sockets.emit() broadcasts to everybody currently connected to your server via socket. That's the reason it works. Technically it's because that's a 'shortcut' for io.of('').emit() ('' being the namespace).

Assuming you're going to use the /hello namespace, this is what you have to do on your client:

var socket = io.connect('http://localhost:3000/hello'); // your namespace is /hello

on the server you first have to listen for connections on that namespace:

io.of('/hello').on('connection', function(socket) {
  socket.emit('world', { a: 'hi world' });
});

then:

io.of('/hello').emit('something');

You may want to look at these: socket.io: How to use and socket.io rooms on GitHub

### UPDATE ###

I conducted a little test:

client:

$('document').ready(function() {
  var socket = io.connect("localhost:3000/hello");

  socket.on('hello', function() {
    console.log('hello received');
  });

  var data = {};
  data.title = "title";
  data.message = "message";

  setTimeout(function() {
    $.ajax({
      type: 'POST',
      data: JSON.stringify(data),
      contentType: 'application/json',
      url: 'http://localhost:3000/hello',
      success: function(data) {
        console.log('success');
        console.log(JSON.stringify(data));
      }
    });
   }, 2000);
});

server:

io.of('/hello').on('connection', function() {
  console.log("client connected");
});

app.post('/hello', function(req, res) {
  io.of('/hello').emit('hello');
});

... and it worked. I copied the jquery-ajax code from here.

DifferentPseudonym
  • 1,004
  • 1
  • 12
  • 28
Sphygmomanometer
  • 595
  • 5
  • 10
  • @AriesDevil: Is that your entire code? I'm suspecting you're not listening anywhere for connections on that namespace. See my updated answer. – Sphygmomanometer May 21 '14 at 07:34
  • I use your updated answer but it still not works unfortunatly.See my updated answer.My requirement is when I accept a http request then I emit a event and client listen this event and display data. – AriesDevil May 21 '14 at 09:28
  • @AriesDevil: I updated my answer once more with the code of my little local test. Hope it helps. – Sphygmomanometer May 21 '14 at 12:56
  • 1
    why would you iterate with ajax to catch a response, this breaks all the purpose of socket.io which is to emit events to feed data, if you are going to use an interval do it from the server so you can test the emit of data. – Jose Villalobos Oct 19 '16 at 16:56
  • @Joe Walker: I'm not really sure what you mean. OP said he was receiving an HTTP request and subsequently tried to emit to a namespace. That's exactly what I did. I only used the timeout to delay the HTTP request and the socket event _is_ coming from the server. – Sphygmomanometer Mar 14 '17 at 09:03