I'm using node.js to have multiple clients. Now, in my code, I'm listening on a port and each time a client connects, I want to send out a broadcast message to all other clients, say. I'm raising a new event each time some new client connects, and I'm sending a response over to him. When I say response.end(), I'm not able to send anything to that specific client after that. However, the browser keeps loading and waiting for the response.end. Is there anyway around this without using socket.io? I know socket.io has its uses, but I really wanted to know if I can do it without socket.io.
3 Answers
I'd say to use two channels in your client; one to receive updates (channel kept open, or frequently querying for updates) and one to refresh the data. It involves some work on your part to restructure the client and the server, but I think using two channels is the cleanest way to solve your problem.

- 38,903
- 3
- 77
- 117
-
Thanks for the quick reply, but either way I would have to specify a response.end(), and that seems to end my connection each time. So, I'm not able to send out messages to the client again after that. If I keep refreshing, instead of node.js I could just have a normal server and clients polling every now and then right? So, I'm trying to avoid that. By two channels, do you mean all the clients are on both channels? – wittythotha Jul 25 '11 at 20:36
If you don't send the .end() the browser will keep waiting. But if you close the channel you'll not be able to send nothing through it. So, either use socket.io or open a channel via XHR and keep it open for each client to receive messages. Is up to you to adequately frame the messages (implement your applicative protocolo) though. I think Paul is right.

- 5,740
- 5
- 33
- 40
-
right, I'm trying to do it using XHR like you said, and keeping it open. But for XHR too, I would just say connection = keep-alive right? Is there anywhere else where I would have to specify to open it using XHR? – wittythotha Jul 25 '11 at 20:39
-
connection: keep-alive is a header you can specify opening the connection, but if you're uising jQuery I think it is already set for you. – Claudio Jul 25 '11 at 20:44
Try long polling, which means letting an ajax request from the browser 'hang' until the server can send something back.
How do I implement basic "Long Polling"?
Using nodejs, you could store the response objects for the clients easily.