Skip to content

Commit

Permalink
moving forward
Browse files Browse the repository at this point in the history
+ added example page
+ implemented communication interface
  • Loading branch information
mainiak committed Oct 3, 2013
1 parent 441f45b commit 68709f2
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 9 deletions.
34 changes: 34 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<html>
<head>
<title>SSE test</title>
</head>
<body>
<h1>SSE test</h1>
<p>See your javascript console.</p>
<script type="text/javascript">
if (!!window.EventSource) {
var source = new EventSource('/sse-events/');

source.addEventListener('message', function(e) {
console.log(e.data); // XXX
}, false);

source.addEventListener('open', function(e) {
// Connection was opened.
console.log('new connection'); // XXX
}, false);

source.addEventListener('error', function(e) {
if (e.readyState == EventSource.CLOSED) {
// Connection was closed.
console.log('closed connection'); // XXX
}
}, false);

} else {
// Result to xhr polling :(
console.log('We should do xhr polling :-/');
}
</script>
</body>
</html>
38 changes: 38 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var Hapi, hostname, permissions, options, port, server, staticRoute;

Hapi = require('hapi');

hostname = '0.0.0.0';
port = process.env.PORT || 8000;
port = parseInt(port, 10);

server = Hapi.createServer(server, port);

permissions = {};
options = {
sse: '/sse-events/'
};
server.pack.allow(permissions).require(__dirname + '/..', options, function(err) {
if (err) {
console.err("Failed to load plugin.");
throw err;
}
});

staticRoute = {
method: 'GET',
path: '/{path*}',
handler: {
directory: {
path: __dirname,
listing: false,
index: true
}
}
};

server.route([staticRoute]);

server.start(function() {
return console.log("# Started at " + server.info.uri);
});
2 changes: 2 additions & 0 deletions example/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
node index.js
25 changes: 25 additions & 0 deletions lib/HapiSSE.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class HapiSSE

constructor: ->
@_users = {}

isOnline: (user) ->
return @_users[user]?

sendMsgTo: (user, msg) ->
if not @isOnline user
throw new Error 'user is offline'
data = 'data: ' + msg + "\n\n"
@_users[user].write data
#console.log data # XXX

_addUser: (user, stream) ->
@_users[user] = stream
#console.log 'adding ' + user # XXX

_delUser: (user) ->
@_users[user].end()
delete @_users[user]
#console.log 'removing ' + user # XXX

module.exports = HapiSSE
35 changes: 35 additions & 0 deletions lib/HapiSSE.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 38 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Load modules
var Hoek = require('hoek');
var HapiSSE = require('./HapiSSE');
var PassThrough = require('stream').PassThrough;
var util = require('util');

// Declare internals
var internals = {};
Expand All @@ -9,6 +12,8 @@ internals.defaults = {
sse: '/sse'
};

hapiSSE = new HapiSSE();

// SSE
exports.register = function (plugin, options, next) {

Expand All @@ -18,15 +23,39 @@ exports.register = function (plugin, options, next) {
plugin.route({
method: 'GET',
path: settings.sse,
handler: function () {
this.reply('data: "Hello SSE World!!!"').type('text/event-stream').header('Cache-Control','no-cache').header('Connection','keep-alive');
/* // FIXME - this doesn't work (closing request with line above?)
var request = this;
var t = setInterval(function() {
console.log('Send data');
request.reply("data: DATA\n\n").type('text/event-stream').header('Cache-Control','no-cache').header('Connection','keep-alive');
}, 1000);
*/
handler: function (request) {
var channel = new PassThrough();
channel.write(''); // to start reading at any time

var remoteIP = request.raw.req.socket.remoteAddress;
//console.log(util.inspect(remoteIP)); // XXX

hapiSSE._addUser(remoteIP, channel);

// detect remote side has closed connection
request.raw.req.on('close', function() {
console.log(remote, arguments); // XXX
hapiSSE._delUser(remoteIP);
});

hapiSSE.sendMsgTo(remoteIP, 'Welcome!'); // XXX

request.reply(channel).type('text/event-stream').header('Cache-Control','no-cache').header('Connection','keep-alive');

// XXX
setTimeout(function() {
hapiSSE.sendMsgTo(remoteIP, 1);
}, 5000); // 5 secs

setTimeout(function() {
hapiSSE.sendMsgTo(remoteIP, 2);
}, 10000); // 10 secs

setTimeout(function() {
hapiSSE.sendMsgTo(remoteIP, 3, true);
hapiSSE._delUser(remoteIP);
}, 20000); // 20 secs
// XXX
},
config: {
description: "ServerSideEvent plugin for Hapi.js"
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@
"license": "BSD-2-Clause",
"dependencies": {
"hoek": "~1.0.3"
},
"devDependencies": {
"hapi": "~1.10.0",
"hoek": "~1.0.3",
"coffee-script": "~1.6.3"
}
}

0 comments on commit 68709f2

Please sign in to comment.