please advice someting. Client code:
var source = new EventSource('/notifications/stream');
source.addEventListener('open', function(e) {
console.log('SSE connected')
}, false);
source.addEventListener('message', function(e) {
alert('yes')
console.log('SSE connected')
}, false);
Server code
// set timeout as high as possible
req.socket.setTimeout(Infinity);
// send headers for event-stream connection
// see spec for more information
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
res.write('\n');
setInterval(function(){
console.log('writing');
res.write("id: " + Date.now() + "\n");
res.write("data: " + '{"message":"hello world"} ' + "\n\n");
}, 1000);
And nothing works... but if I add res.end()
after res.write()
client fires alert()
for one time and server crashes because it already ended the res
. I read that it might be antivirus's problem that I client doesn't fire until connection is closed, so I turned off antivirus but it didn't help. So my question is:
- Is it code problem or other reason (antivirus)
- if it's from antivirus, is it for clients antivirus or server's antivirus problem? (I'm developing on Windows with Nod32 antivirus, if I will deploy on linux, will it help or it depends from client's antivirus)
- Is there alternative but websocket (my only need is to push notifications with minimum memory usage)
NodeJS version 0.10.28 Thank You for attention. Sorry for bad English.