Sorry, noticed that you want them sent at the same time:
var clients = [];
require('http').createServer(function (req, res) {
if (req.headers.accept && req.headers.accept == 'text/event-stream') {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
clients.push(res);
req.on('close', function reqClosed() {
clients.splice(clients.indexOf(res), 1);
});
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('<!DOCTYPE html><html><head>' +
'<script>' +
'new
EventSource("/").addEventListener("message",function(event) {' +
'document.getElementById("sse").innerHTML=event.data' +
'});' +
'</script>' +
'</head><body><div id="sse">Waiting...</div></body></html>');
res.end();
}
}).listen(9080);
setInterval(function sse() {
if (!clients.length) {
return;
}
var dt = new Date().toUTCString();
for (var i = 0; i < clients.length; i++) {
clients[i].write("data: " + dt + '\n\n');
}
}, 5000);
On Thursday, November 20, 2014 10:11:48 AM UTC-5, Will Hoover wrote:
>
> Just to add to Ryan's comments...
>
> If you do decide to use a modern web browser as your client (and you don't
> care about Internet Exploder support) you can use SSEs. The beauty of using
> SSE is that if your server goes down or a connection is lost the SSE client
> will reconnect automatically:
>
> require('http').createServer(function (req, res) {
> if (req.headers.accept && req.headers.accept == 'text/event-stream') {
> res.writeHead(200, {
> 'Content-Type': 'text/event-stream',
> 'Cache-Control': 'no-cache',
> 'Connection': 'keep-alive'
> });
> var id = 0;
> setInterval(sse, 5000);
> // or: process.nextTick(sse);
> sse();
> function sse() {
> res.write('id: ' + ++id + '\n');
> res.write("data: " + new Date().toUTCString() + '\n\n');
> }
> } else {
> res.writeHead(200, { 'Content-Type': 'text/html' });
> res.write('<!DOCTYPE html><html><head>' +
> '<script>' +
> 'new
> EventSource("/").addEventListener("message",function(event) {' +
> 'document.getElementById("sse").innerHTML=event.data'
> +
> '});' +
> '</script>' +
> '</head><body><div id="sse">Waiting...</div></body></html>');
> res.end();
> }
> }).listen(9080);
>
>
> On Thursday, November 20, 2014 7:46:31 AM UTC-5, ryandesign wrote:
>>
>>
>> On Nov 20, 2014, at 4:02 AM, Niral Kalavadia wrote:
>>
>> > Yes it is necessary for server to ask for data to client. This is the
>> only mandatory need of my network. Is it possible to use node.js in this
>> application?? Is node capable of sending request to multiple client at a
>> time???
>>
>> Yes, and yes. You can write any code you want to, in most any language.
>> :)
>>
>> If your server is an http server written in node, and your client is a
>> modern web browser, then the already-mentioned socket.io library is a
>> popular way to achieve this goal. If your client and server are not talking
>> http to one another, if your client is not a modern web browser, then you
>> may need a different library, or you may need to write the communication
>> code yourself.
>>
>>
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/5624ecaa-62f2-4132-954f-ce9c4c8c51e8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.