Skip to content

Instantly share code, notes, and snippets.

@Fabryz
Created April 3, 2011 10:04
Show Gist options
  • Save Fabryz/900335 to your computer and use it in GitHub Desktop.
Save Fabryz/900335 to your computer and use it in GitHub Desktop.
NowJS Introduction: Simple Chat Server in 12 Lines of Code
<!DOCTYPE html>
<html>
<head>
<title>NowJS test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script src="/nowjs/now.js"></script>
<script>
$(document).ready(function() {
now.name = prompt("What's your name?", "");
now.receiveMessage = function(name, msg) {
$("#msg").append("<br/>" + name + ": " + msg);
};
$("#send-button").click(function() {
now.distributeMessage($("#text-input").val());
$("#text-input").val("");
});
});
</script>
</head>
<body>
<div id="msg"></div>
<input type="text" id="text-input">
<input type="button" value="Send" id="send-button">
</body>
</html>
var http = require('http'),
url = require('url'),
sys = require('sys'),
fs = require('fs');
server = http.createServer(function(req, res) {
var path = url.parse(req.url).pathname;
console.log("* HTTP request: "+ path);
switch (path) {
default:
res.writeHead(200, { 'Content-Type': 'text/html' });
var rs = fs.createReadStream(__dirname + '/index.html');
sys.pump(rs, res);
break;
}
});
server.listen(8080);
console.log('Server started.');
var everyone = require("now").initialize(server);
everyone.now.distributeMessage = function(msg) {
everyone.now.receiveMessage(this.now.name, msg);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment