-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
broadcast doesn't work in namespace #291
Comments
Yes, I stumbled uppon it too. I guess, such broadcasting is a bit incorrect. Logically it seems, the socket (which comes as an argument af connection event) should be able only to send message to the connected client and not to all of the clients of the namespace. For a simple 'send' it seems this is the way it works - if I do the socket.send(), only the connected client gets the message, and this is the behaviour I expected. var chat = ioserver.of('/chat');
chat.on('connection', function (socket) {
console.log('chat connection');
socket.on('message', function (msg) {
console.log('chat message');
chat.broadcast.send(msg);
});
}); I get an exception: BTW, isn't there the same issue related error on the socket.io page in this example: var io = require('socket.io').listen(80);
var chat = io
.of('/chat'); // <-- BTW, node.js says - syntax error ; because . follows on the next line
.on('connection', function (socket) {
socket.emit('a message', {
that: 'only'
, '/chat': 'will get'
}); // <-- isn't this supposed to be 'a message that only a single socket will get'?
chat.emit('a message', {
everyone: 'in'
, '/chat': 'will get'
}); // <--, ok, this works as advertised, but the problem - broadcast is not defined for this namespace, so no broadcasting possible
}); Anyway, thanks for the great socket.io library and I hope someone will make the situation clear about what sending is on a single socket and what works for the entire namsepace. |
Looking in to it, thanks for reporting. @midix you have a different issue than @outsideris so I suggest creating a new issue for that. |
My problem actually is the same as for @outsideris - currently there is no working way how to broadcast a message on a namespace. I just additonally pointed out that 'broadcast' flag is defined on the 'socket' object and there is no 'broadcast' on the namespace object (where 'broadcast' logically should belong). |
@midix node does not give a syntax error ; because it's on the next line, but because you have a outsideris is having a with the |
I tested more function. |
Isn't the socket.emit and namespace.emit the same idea that socket.broadcast.emit() and namespace.broadcast.emit() except that broadcast does not echo message back to the sender? Then the problem is the same: socket.broadcast.emit is sending to no-one (currently socket.emit sends only to itself and broadcasting means "ignore myself", so obviously we have a pretty logical exclusion with the result - send to no-one) and namespace.broadcast.emit gives 'undefined' for 'broadcast', so broadcast is not actually working. I do not consider the problem that socket.emit sends only to a single client and namsepace.emit is sending to the entire namespace - I thought this behaviour was implemented intentionally but the website example was incorrect. About having a ; after of(chat) and doing a .on after it - this is what I copied from the socket.io web site namespaces example, so I just pointed out this little error. Anyway, to avoid further confusion, socket.io website should state more clearly, when we should use socket.emit/send and when namespace.emit/send and which of those two is responsible for (currently broken) broadcasting. |
Well, Client connections are bound to server-side namespaces which you choose by means of So when you do...
Then if you want to broadcast to "/chat" namespace, you use: For that reason, you can do broadcast using namespace methods, which, however, have no knowledge which socket to exclude, and hence broadcast will go to every socket which belongs to the namespace. |
For us to quicker fix it, a gist with stripped down server and client code demonstrating the issue (presumably not depending on express-es, jquery-es et al.) is very welcome. |
@dvv: thanks for the clarification, now I understand why 'broadcast' is defined for the socket and not for the namespace. Does this mean that
If point 1) is true (at least that was how it worked when I tested it 4 days ago), then that example on the socket.io website: socket.emit('a message', { should be something more like this: socket.emit('a message', { I'll put together and share my minimalistic server/client example with broken broadcasting after 3-4 hours. |
@midix: you are right on all points 1-3. I'm sure you understand that during such major refactoring website can contain both oldies and typos. Waiting for the example, |
Here it is, packed together with the socket.io: http://filepost.com/files/4mbfbdd1/socket_io_broadcast_issue.tar.gz/ At first I wanted to check the latest git version from the master branch to see if something has changed, but it gave me 'Object has no method 'Listen'" so I stayed with version that "npm install socket.io" gave me. The server is a basic triple echo server, which sends the message to all, then back to the sender and then broadcasts it. The client is basically the same chat html file as with earlier socket.io examples, I just ripped out all unneeded staff and added a namespace. I could run it without problems directly in Chrome and Firefox from a local file. So when opening test.html in Chrome and Firefox, I could see the messages between browsers sent only once but they should be sent twice because of the 'send' followed by a 'broadcast' on the server. If you comment out the chat.send(msg) line, the messages between both browsers won't work at all - so the broadcast is not sending anything. |
thanks. |
np. |
of course!! they will. |
I also couldnt get the broadcast to work over namespaces either. The 2 scripts are very simple and standalone, "npm install socket.io" at root and put client in a "http" folder. You can also verify the test for non-namespace socket by just commenting/decommenting the lines "io.of/io.sockets" for server and the connect lines for the client. The debug clearly shows that the server is "broadcasting packet" for both clients (Webscoket & flashsocket) when they initially connect. But the clients do not fire the event... Do they even get it? I dont know... any debug I could enable on client side to see the traffic at the socket.io level? Really hope this gets fixed soon. |
This fixes the problem: If the room was empty it was adding an extra slash at the end of the endpoint and for some reason that messed up things. |
socket.io v0.7.6: Namespace works on Chrome 12.0.742.91 with websocket and on Firefox 5.0 with flashsocket. If you don't configure the transports option, socket.io defaults to xhr-polling on Firefox and then it does not work. |
@gametbt Still isnt fix in 0.7.6 I had to re-apply my fix locally after an upgrade. |
@Bwen writing a testcase against it atm so if i can confirm it works I will create patch for it. |
@Bwen my statement was in regard to the broadcast to all in the namespace via "emit". I have not tried the 'rooms' yet. |
I wrote some examples. but it didn't work in my code. Server : https://github.com/outsideris/socket.io-examples/blob/master/app.js there are some examples for test. |
@outsideris Instead of packing all features in one app, why don't you create separate app for each feature and post those that you have trouble with. Small code fragments will allow you, and anybody that you seek help from, spot the issues easily and quickly. |
@gametbt I got it. I will write code fregments. |
I wrote some examples example 1 : https://gist.github.com/1068731 broadcast.send & broadcast.emit is not work. I don't know why. express v2.4.1 |
It's already patched in a pull request I made, see referenced commit above, or go directly to pull request: #340 |
@3rd-Eden |
Landed in master |
thanks to everybody. |
message event is fired...
but it can't broadcast. and
broadcast.emit
too..I use socket.io .0.7.2 and node. 0.4.8
The text was updated successfully, but these errors were encountered: