Skip to content

Instantly share code, notes, and snippets.

@cayasso
Created March 12, 2014 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cayasso/9510749 to your computer and use it in GitHub Desktop.
Save cayasso/9510749 to your computer and use it in GitHub Desktop.
Primus Quick Example
<!Doctype html>
<html>
<head>
<script src="/primus/primus.js"></script>
</head>
<body>
<script>
var primus = new Primus('ws://localhost:5000', { transformer: 'engine.io' });
primus.on('open', function () {
console.log('OPEN');
});
primus.on('reconnect', function () {
console.log('RECONNECT');
});
primus.on('reconnecting', function () {
console.log('RECONNECTING');
});
</script>
</body>
</html>
{
"name": "example",
"version": "0.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"primus": "2.0.2",
"engine.io": "~1.0.1"
}
}
var Primus = require('primus')
, http = require('http')
, fs = require('fs');
var server = http.createServer(function server(req, res) {
res.setHeader('Content-Type', 'text/html');
fs.createReadStream(__dirname + '/index.html').pipe(res);
});
var primus = new Primus(server, { transformer: 'engine.io' });
primus.on('connection', function (spark) {
console.log('CONNECTING ===>', spark.id);
});
// Start server listening
server.listen(process.env.PORT || 5000, function(){
console.log('\033[96mlistening on localhost:5000 \033[39m');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment