node-channel
Browserchannel is a protocol developed by google in order to create a bidirectional, low latency channel of communication between browser and server over http.
The protocol provides two virtual channel, backward and forward.
The client part of the protocol is implemented primarly in google's opensource closure library.
As the server side implementation used by google is not yet opensourced, browserchannel-middleware is a free implementation written as a express.js middleware.
Please note this must be considered as a less-than-beta version. Maybe it can be used in the real world and maybe will bring memory leaks, every kind of bug or velociraptor attacks. This is the beauty of open source..
The tests are written with mocha testing tool. If you are interested on improve this library or find a bug please let me know.
Table of compatibility
Browser | Mode |
---|---|
IE 10 | xhrstreaming |
IE < 10 | forever frame |
IE =< 6 | not tested |
Chrome | xhrstreaming |
Safari | xhrstreaming |
Firefox | xhrstreaming |
Chrome android | xhrstreaming |
Safari IOS | xhrstreaming |
Thanks to ahochhaus for documenting the protocol and to josephg for his coffescript implementation, that gave me a good point to start.
Example usage
(please note that the "example" directory contains a full broadcast chat example with browser and server code)
Request handler (Handler.js)
browserchannel = ; /** * Browserchannel Handler class * * @constructor * @extends */ { Handlersuper_; /** * An associative array hoding sessions * * @type */ thissessions = {};};util; /** * Return the number of current managed sessions * * @return */Handlerprototype { return Objectlength;}; /** * Iterate and calls callback through sessions * * @param */Handlerprototype { for var key in thissessions if thissessions cb; }; /** * Writes to backward channel stream * * @param * @param */Handlerprototype { session;}; /** * Called when browser send a message to server to this session * * @param * @param */Handlerprototype { // broadcast message this;}; /** * Called when browser disconnects * * @param */Handlerprototype { var res = delete thissessionssessionid;}; /** * Called once when channel is ready * * @param */Handlerprototype { this; thissessionssessionid = session;}; moduleexports = Handler;
Server (server.js)
browserchannel = ;util = ;express = ;Handler = ; // Server bootstrapapp = ; // register middlewaresapp;app; // serve static filesapp; // browserchannel optionsbrowserchannel_opts = debug: false // this will print A LOT of informations of what is happening handler: // register handler; // register middlewareapp; // register a route, just to see it is handled outside browserchannelapp; // start serverport = 4444; console;app;