Last active
December 17, 2015 03:08
-
-
Save dvberkel/5540852 to your computer and use it in GitHub Desktop.
Experiment with Faye
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
var express = require('express'); | |
var http = require('http'); | |
var Faye = require('faye'); | |
var app = express(); | |
app.use(express.static(__dirname + '/public')); | |
var bayeux = new Faye.NodeAdapter({ mount : '/faye' }); | |
var server = http.createServer(app); | |
var port = process.env.PORT || 3435; | |
server.listen(port); | |
bayeux.attach(server); | |
console.log("listening on port " + port); |
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
<!doctype html> | |
<html> | |
<head> | |
<meta charset='utf8'> | |
<title>faye experiment</title> | |
<script type='text/javascript' src='/faye/client.js'></script> | |
<script type='text/javascript' src='/js/setup.js'></script> | |
</head> | |
<body> | |
<input id='message' type='text'/><button id='send'>send</button> | |
<ul id="messages"> | |
</ul> | |
</body> | |
</html> |
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
{ | |
"name": "faye-experiment", | |
"version": "0.0.0", | |
"description": "Faye experiment", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"repository": "", | |
"author": "dvberkel", | |
"license": "BSD", | |
"dependencies": { | |
"faye": "~0.8.9", | |
"express": "~3.2.3" | |
}, | |
"devDependencies": { | |
"nodemon": "~0.7.8" | |
} | |
} |
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
(function(Faye, undefined){ | |
var client = new Faye.Client('/faye'); | |
client.subscribe('/messages', function(message){ | |
var li = document.createElement('li'); | |
li.textContent = message.text; | |
var messages = document.getElementById('messages'); | |
messages.appendChild(li); | |
}); | |
window.onload = function(){ | |
var button = document.getElementById('send'); | |
button.addEventListener('click', function(event){ | |
var message = document.getElementById('message'); | |
client.publish('/messages', { | |
text: message.value | |
}) | |
}); | |
}; | |
})(Faye) |
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
. | |
├── app.js | |
├── package.json | |
└── public | |
├── index.html | |
└── js | |
└── setup.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment