Giter Site home page Giter Site logo

michel / node.websocket.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rauchg/node.websocket.js

1.0 2.0 1.0 87 KB

WebSocket-compatible realtime HTTP server implemented with Node.JS

Home Page: http://devthought.com/blog/2009/12/nodejs-and-the-websocket-protocol/

node.websocket.js's Introduction

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

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=['http://some_allowed_host']

The option values are eval()'d to turn them into native JavaScript types, so don't forget to wrap strings in ' '.

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!

TODO

  • Support for WebSocket-Protocol header, and additional HTTP headers such as Cookie. 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 <http://devthought.com>

node.websocket.js's People

Contributors

rauchg avatar adamac avatar

Stargazers

Michel de Graaf avatar

Watchers

Michel de Graaf avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.