I want to create a chat room using HTML5 web sockets, but am pretty lost. From what I can tell, the following is required:
- A browser that supports web sockets (Chrome, Safari)
- Some kind of server-side scripting
- Some kind of client-side scripting
I've got #1 down :) but #2 is seriously tripping me up. Beyond amateur PHP work (generally within the context of Drupal), I have pretty much no experience coding on the server-side of things. I tend to code a lot in Javascript, and have read fantastic things about node.js. So I thought I'd try using it to play around with web sockets.
I've installed node.js to my Mac Leopard machine (it installs to Home > node), but I really have no idea where to go from there. The node.js website provides a "Hello World" example, which I've tried (I put the code into an example.js file, and saved that in the root of the "node" folder), but I only get the following response in Terminal:
Server running at http://127.0.0.1:8124/
I would LOVE a node.js and web sockets for dummies kind of thing. Thanks for any help that can be provided.
I want to create a chat room using HTML5 web sockets, but am pretty lost. From what I can tell, the following is required:
- A browser that supports web sockets (Chrome, Safari)
- Some kind of server-side scripting
- Some kind of client-side scripting
I've got #1 down :) but #2 is seriously tripping me up. Beyond amateur PHP work (generally within the context of Drupal), I have pretty much no experience coding on the server-side of things. I tend to code a lot in Javascript, and have read fantastic things about node.js. So I thought I'd try using it to play around with web sockets.
I've installed node.js to my Mac Leopard machine (it installs to Home > node), but I really have no idea where to go from there. The node.js website provides a "Hello World" example, which I've tried (I put the code into an example.js file, and saved that in the root of the "node" folder), but I only get the following response in Terminal:
Server running at http://127.0.0.1:8124/
I would LOVE a node.js and web sockets for dummies kind of thing. Thanks for any help that can be provided.
Share Improve this question edited Aug 21, 2010 at 20:56 David 220k41 gold badges233 silver badges327 bronze badges asked Aug 21, 2010 at 20:52 maxedisonmaxedison 17.6k15 gold badges71 silver badges118 bronze badges 5- 1 Check out Jeff Kreeftmeijer's "ghost pointers" experiment. That's effectively the framework you need for realtime chat; you just interpret the data differently clientside. jeffkreeftmeijer./2010/experimenting-with-node-js – Chris Heald Commented Aug 21, 2010 at 20:59
- The "hello world" is the response if you make a web request to 127.0.0.1:8124 – mhitza Commented Aug 21, 2010 at 22:30
- mhitza-- okay. how do i make that request? – maxedison Commented Aug 22, 2010 at 5:31
- start the server and then enter the above 127.0.0.1:8124 URL in your browser. before diving into node.js and websockets, it might be good to get a better understanding of http protocol internals and the asynchronous event model, since they rely heavily on this. – shreddd Commented Aug 22, 2010 at 6:54
- Thanks shreddd. Can you remend any resources on those two subjects? – maxedison Commented Aug 22, 2010 at 12:48
3 Answers
Reset to default 4This may be slightly more advanced but it does provide a decent WebSocket layer for node.js: http://github./LearnBoost/Socket.IO-node
That said, if you haven't done much server-side stuff, it might be better to get a feeling for the http protocol including how a request and response is constructed, how headers are added etc. (outside of node). Once you have a better sense for this, node.js will be much easier to understand.
This tutorial should give you a basic overview: http://www.tutorialspoint./http/index.htm
This stuff bees even more important when dealing with websockets
This is also easy in RingoJs http://ringojs if you want to stick with JavaScript on the serverside. This is the gist of what you write to have a websocket Listener.
var websocket = require("ringo/webapp/websocket");
exports.serverStarted = function(server) {
var context = server.getDefaultContext();
websocket.addWebSocket(context, "/websocket", function (socket) {
// this function, being passed the socket, is called everytime
// a new socket connection is made.
// overwrite onMessage to intercept the messages
socket.onmessage = function(m) {
};
});
};
// send smth to the client
fooSocket.send('your message')
Inside socket.onmessage you just grab the message and work with it. You will probably store the sockets somewhere to have access to them later on.
See:
- http://gist.github./555596 (the example above but more elaborate)
- http://ringojs/api/master/ringo/webapp/websocket/ Websocket Ringo API
There is a demo project on the node.js site with a live chat. http://chat.nodejs/
and the source code is here. http://github./ry/node_chat
this is not using html5 but hope this points you in the right direction.