CARVIEW |
Guille / node.websocket.js
- Source
- Commits
- Network (16)
- Issues (4)
- Downloads (0)
- Wiki (1)
- Graphs
-
Branch:
master
click here to add a description
click here to add a homepage
name | age | message | |
---|---|---|---|
![]() |
.gitignore | Sun Feb 07 06:23:04 -0800 2010 | Added some modules and examples [bru] |
![]() |
README.md | Wed Mar 17 16:09:06 -0700 2010 | README update [Guille] |
![]() |
lib/ | Thu Feb 18 13:29:04 -0800 2010 | Support node.js v0.1.29 API [bru] |
![]() |
log.js | Sun Dec 06 23:59:15 -0800 2009 | Redis is now optional [Guille] |
![]() |
modules/ | Tue Feb 09 23:58:55 -0800 2010 | Added topic, action and quit command to Chat [Riccardo Cambiassi] |
![]() |
runserver.js | Sat Dec 05 23:56:47 -0800 2009 | Initial commit of node.websocket.js. Fully func... [Guille] |
![]() |
test/ | Tue Feb 09 23:58:55 -0800 2010 | Added topic, action and quit command to Chat [Riccardo Cambiassi] |
![]() |
tools.js | Sat Mar 13 15:30:39 -0800 2010 | fixing command line parsing correctly and remov... [jgoulah] |
![]() |
websocket.js | Thu Feb 18 13:29:04 -0800 2010 | Support node.js v0.1.29 API [bru] |
Update: For a more complete solution, I recommend you look into Socket.IO and Socket.IO-node.
node.websocket.js
node.websocket.js is an experimental implementation of the Web Socket protocol for the Evented I/O API Node.js.
The end goal of the project is to provide an abstraction layer of the protocol used to support different communication schemes through a simple-to-use API. As such, node.websocket.js should be able to work with UAs that use alternative connection methods (xhr streaming, long-polling, forever iframe).
Requirements
- Node.js (requires v0.1.29+!)
Optional:
- Redis used for logging. redis-node-client is included. See
log.js
for the benefits of Redis as a logging mechanism.
How to use
Run the server:
$ node runserver.js
By default, it'll listen on localhost port 8080. node.websocket.js interprets the arguments passed in and turns those into the object passed to the websocket::Server
constructor:
$ node runserver.js --port=8080 --host=some_other_host --origins=[https://some_allowed_host]
On the client side, initialize a WebSocket
like this:
new WebSocket(ws://localhost:8080/test);
websocket::Connection
will try to load a module in the modules/ directory with the name of the passed resource (in this case test
).
If the resource is just / (for example ws://localhost:8080/
), modules/_default.js will be loaded. The module has to expose a Module
pseudoclass with an onData method like this:
var Module = this.Module = function(){
// constructor;
};
Module.prototype.onData = function(data, instance){
// do something
};
The second parameter received is the websocket::Connection
instance. To send data back to the client your module should do something like this:
Module.prototype.onData = function(data, connection){
connection.send('sending data!');
}
Additionally, you can implement an onDisconnect
method, called when a Connection
finishes.
Features
Very clean API that you can extend.
It's easy to handle different resources as modules through Node dependency injection.
Support for flash policy requests (for flash-based WebSocket emulation for old browsers). Thanks @joewalnes!
Demonstration
Here's a screenshot of the demo that comes with node.websocket.js:
In order to run it by yourself, download and compile redis and run it in a terminal
$ ./redis-server
While redis is simply used for logs storage here (and its not indispensable), I highly encourage you to discover and examine its potential.
In a different terminal, as described above, run node.webserver.js:
$ node runserver.js
Access test/test.html (which you can run locally or deliver through any web server, such as Apache) and watch true realtime data exchange!
Other Examples
The modules folder contains a few more demos:
- echo.js - a basic echo server
- processes.js - shows the output of a "ps" command every 5 seconds
- chat.js - a port of the nodechat to node.websockets
The try them out, you can use the html fies in test/websockets. Please also note that these integrate web-socket-js to support non-Chrome browsers.
TODO
- Support for
WebSocket-Protocol
header, and additional HTTP headers such asCookie
. WebSocket-Protocol support is optional, so node.websocket is still spec-compliant. - Support for wss:// and TLS handshake
- More strict URI parsing/validation
Author
Guillermo Rauch <https://devthought.com>
Special thanks to [Riccardo Cambiassi]https://github.com/bru for his work on the chat example and API changes.