Created
July 23, 2013 01:04
-
-
Save 1N50MN14/6059074 to your computer and use it in GitHub Desktop.
webinstance/app notification example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
******** client code ******** | |
var Unreal = require('unrealjs') | |
var routes = require('./routes') | |
var app = new Unreal({role:'web', version:'0.0.1'}) | |
app.registerRoutes(routes) | |
app.on('ready', function (remote) { | |
// do this when a user logs in | |
app.setConnectionKey('isLoggedIn', true) | |
}) | |
app.on('data', function(stream, data) { | |
if (stream.meta == 'notification') alert(data) | |
}) | |
app.on('connect', function (w) { | |
console.log('connected to ', w.url + ':' + w.port) | |
}) | |
app.on('disconnect', function (w) { | |
console.log('disconnected from ', w.url + ':' + w.port) | |
}) | |
******** in webinstance ******** | |
// let's say we want to send a "hello world" message to | |
// all logged in users | |
webinstance.on('listening', function (meta) { | |
var conns, self=this | |
// inside the connect scope for demo purposes only this | |
// shouldn't be here | |
self.on('connect', function (connid) { | |
// emitting the event after 1 sec, demo purposes only | |
setTimeout(function(){ | |
// emit a p2p event | |
self.peer.emitEvent('helloEvent', 'hello world!!') // emit a p2p event | |
},1000) | |
// 'helloEvent' received; emit a notification to all connected clients | |
// that are logged in | |
self.peer.on('helloEvent', function(msg){ | |
conns = self.worker.getConnectionIdsByKey('isLoggedIn', true); // can also do this.getConnectionsById | |
self.worker.createWriteStream('notification', conns).write(msg); | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment