Reputation: 17573
I want to create a chat room using HTML5 web sockets, but am pretty lost. From what I can tell, the following is required:
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.
Upvotes: 3
Views: 2385
Reputation: 1361
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:
Upvotes: 2
Reputation: 11413
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
Upvotes: 4
Reputation: 3147
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.
Upvotes: 1