Last active
August 29, 2015 14:20
-
-
Save lpinca/dac8f2efca54518555a4 to your computer and use it in GitHub Desktop.
Broadcasting messages to clients that connect to different servers
This file contains hidden or 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
'use strict'; | |
var Socket = require('primus').createSocket({ transformer: 'faye' }) | |
, one = new Socket('http://localhost:3001') | |
, two; | |
one.on('data', function (data) { | |
console.log('"one" received message: '+ data); | |
}); | |
one.on('open', function () { | |
two = new Socket('http://localhost:3002'); | |
two.on('data', function (data) { | |
console.log('"two" received message: '+ data); | |
}); | |
two.write('this is a message for all connected clients'); | |
}); |
This file contains hidden or 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
{ | |
"name": "broadcast", | |
"version": "1.0.0", | |
"description": "", | |
"main": "server.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"faye-websocket": "^0.9.4", | |
"metroplex": "0.0.9", | |
"omega-supreme": "0.0.5", | |
"primus": "^3.0.2" | |
} | |
} |
This file contains hidden or 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
'use strict'; | |
var Primus = require('primus') | |
, http = require('http'); | |
(function startServer(count) { | |
if (!count) return; | |
var server = http.createServer() | |
, primus = new Primus(server, { transformer: 'faye' }); | |
primus.use('omega-supreme', 'omega-supreme'); | |
primus.use('metroplex', 'metroplex'); | |
primus.on('connection', function (spark) { | |
console.log(spark.id + ' connected'); | |
spark.on('data', function (data) { | |
primus.metroplex.servers(function (err, servers) { | |
if (err) throw err; | |
primus.forward(servers, data, function (err, data) { | |
if (err) throw err; | |
console.log(data); | |
}); | |
}); | |
}); | |
}); | |
primus.on('disconnection', function (spark) { | |
console.log(spark.id + ' disconnected'); | |
}); | |
server.listen(3000 + count, '0.0.0.0', function () { | |
var bound = server.address(); | |
console.log('server listening on '+ bound.address +':'+ bound.port); | |
}); | |
startServer(--count); | |
})(2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment