Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A server-side companion for EventSource

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 eventstream.js
Octocat-spinner-32 package.json
Octocat-spinner-32 readme.textile
readme.textile

node-eventstream

A server-side companion for EventSource

Install

npm install eventstream

Example

var es = require('eventstream');
var server = require('http').createServer(function(req, res) {
    
    // Check if this is an event stream request
    if (req.url === '/my-event-stream') {
    
        // Create an EventStream object and link it to the request/response
        var eventStream = new es.EventStream(req, res);
        
        // Start sending keep-alive messages every 15 seconds
        (function keepAlive() {
            if (eventStream.isOpen()) {
                eventStream.keepAlive();
                setTimeout(keepAlive, 15000);
            }
        }());
        
        // Send a simple message event
        eventStream.sendMessage({
            data: 'This is a message'
        });
        
        // Send a more complex message
        eventStream.sendMessage({
            event: 'message',
            id: 'some id value',
            data: 'This is another message'
        });
    
    }
    
    // Otherwise, continue on with the rest of your normal server stuff...
    
});

server.listen(1234);
Something went wrong with that request. Please try again.