Skip to content

Instantly share code, notes, and snippets.

@matthewfl
Created November 24, 2010 06:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save matthewfl/713200 to your computer and use it in GitHub Desktop.
Save matthewfl/713200 to your computer and use it in GitHub Desktop.
A really basic chat server on http://chat.jsapp.us
<html>
<head>
<title>Chat</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script>
$(function () {
function check () {
$("#output").load('/update', function () {
setTimeout(check, 1);
});
}
function send () {
$.post("/send", $("#text").val());
$("#text").val('');
}
check();
$("#send").click(send);
$("#text").keypress(function (e) {
if(e.keyCode == 13) send();
})
});
</script>
</head>
<body>
<h3>This is a really simple demo that I wrote to quickly test out jsapp.us, the code is only about 15 lines longs and uses node-router</h3>
<p>I came back and added long polling, it took about 15 min</p>
<div id="output">{text}</div>
<br>
<input id="text"></input><button id="send">Send</button>
<br>
<a href="https://gist.github.com/713200">Code</a>
</body>
</html>
// chat application
var db = require('db');
var router = require('matthewfl/node-router'), // this could be ./node-router if you have node-router save in you jsapp filesystem
server = router.getServer(),
fs = require('fs');
var updates = [];
var clients = [];
var fileContent = null;
server.get('/', function (req, res) {
if(fileContent) {
return fileContent.replace("{text}", "<p>"+updates.join("</p><p>")+"</p>")
}else{
fs.readFile('chat.index.html', function (err, content) {
res.writeHead(200, {"Content-type": "text/html"});
res.end(content.replace("{text}", ""));
fileContent = content;
});
}
});
server.get('/update', function (req, res) {
res.writeHead(200, {"Content-type":"text/html"});
clients.push(res);
});
server.post('/send', function (req, res) {
var data="";
req.on('data', function (d) {
data += d.toString('ascii');
});
req.on('end', function () {
updates.push(data);
res.end();
var t = "<p>"+updates.join("</p><p>")+"</p>";
while(clients.length) {
clients.pop().end(t);
}
})
});
server.listen(8000);
@xxxazxxx
Copy link

Hey when i try to run chat.js on JSApp.us i get an error "./node-router not found".
Any help will be appreciated.

@matthewfl
Copy link
Author

Sorry about that, './node-router' is attempting to load node-router out of your local jsapp file system. There are two ways to fix this, the first is to save node-router into a file called node-router on your account. The second is to replace it with 'matthewfl/node-router'. The reason this second way works is that I have decided to make my node-router file public, and thus anyone can import that file.

To control what files are public use the files command in the editor.

@Cramertech
Copy link

I know this is really old - but the chat example page doesn't work.. .. and to top it off, a few minutes after trying it, the page is now giving a server error.

@jay-skeer
Copy link

doesn't work for me too!

@jay-skeer
Copy link

every comment goes to the bit bucket, and doesn't show on my screen

@jay-skeer
Copy link

does there need to be two (or more) connections to the server for it to work?

@jay-skeer
Copy link

now i see 'server error' message, no chat app

@jay-skeer
Copy link

now i see a chat app, that sends every comment to the bit bucket (again)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment