views:

176

answers:

3

I want to create a chat room using HTML5 web sockets, but am pretty lost. From what I can tell, the following is required:

  1. A browser that supports web sockets (Chrome, Safari)
  2. Some kind of server-side scripting
  3. 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.

+1  A: 

There is a demo project on the node.js site with a live chat. http://chat.nodejs.org/

and the source code is here. http://github.com/ry/node_chat

this is not using html5 but hope this points you in the right direction.

Bjarki Heiðar
I've seen that, but I don't know what to do with it. As I said in the original post, I don't even understand the Hello World example.
maxedison
+3  A: 

This may be slightly more advanced but it does provide a decent WebSocket layer for node.js: http://github.com/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.com/http/index.htm

This stuff becomes even more important when dealing with websockets

shreddd
+1  A: 

This is also easy in RingoJs http://ringojs.org 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:

oberhamsi