Skip to content

Latest commit

 

History

History
169 lines (122 loc) · 4.98 KB

File metadata and controls

169 lines (122 loc) · 4.98 KB

A BrowserChannel server.

tldr; Its like socket.io, but it scales better and it has fewer bugs. It just does long polling. It doesn't support websockets and doesn't work cross-domain.

BrowserChannel is google's version of socket.io from when they first put chat in gmail. Unlike socket.io, browserchannel provides much better guarantees about message delivery and state. It has better reconnection logic and error handling. With browserchannel, you know whats going on.

Build Status

node-browserchannel:

  • Is compatible with the closure library's browserchannel implementation
  • Is super thoroughly tested
  • Works in IE5.5+, iOS, Safari, Chrome, Firefox, and probably others.
  • Works in any network environment (incl. behind buffering proxies)

Use it

# npm install browserchannel

Browserchannel is implemented as connect middleware. Here's an echo server:

browserChannel = require('browserchannel').server
connect = require 'connect'

server = connect(
  connect.static "#{__dirname}/public"
  browserChannel (session) ->
    console.log "New session: #{session.id} from #{session.address} with cookies #{session.headers.cookie}"

    session.on 'message', (data) ->
      console.log "#{session.id} sent #{JSON.stringify data}"
      session.send data

    session.on 'close', (reason) ->
      console.log "Session #{session.id} disconnected (#{reason})"
      
    # This tells the session to stop trying to connect
    session.stop()
    
    # This just kills the session.
    session.abort()
).listen(4321)

console.log 'Echo server listening on localhost:4321'

The client emulates the websocket API. Here is a simple client:

{BCSocket} = require 'browserchannel'

socket = new BCSocket 'http://localhost:4321/channel'
socket.onopen = ->
  socket.send {hi:'there'}
socket.onmessage = (message) ->
  console.log 'got message', message

# later...
socket.close()

... Or from a website:

<html><head>
<script src='/channel/bcsocket.js'></script>
<script>
socket = new BCSocket('/channel');
socket.onopen = function() {
  socket.send({hi:'there'});
  socket.close();
};
socket.onmessage = function(message) {
  // ...
};
</script>

You can also ask the client to automatically reconnect whenever its been disconnected. - Which is super useful.

{BCSocket} = require 'browserchannel'
socket = new BCSocket 'http://localhost:4321/channel', reconnect:true
socket.onopen = ->
  socket.send "I just connected!"

Caveats

  • It doesn't do RPC.
  • It doesn't work in cross-origin environments. Put it behind nginx or varnish if you aren't using nodejs to host your whole site.
  • Currently there's no websocket support. So, its higher bandwidth than socket.io running on modern browsers.

How to rebuild the client

The client uses google's closure library & compiler. There's a couple small bugs that google still hasn't fixed in their library (and probably never will), so I have a patch file kicking around.

Rebuilding the client library is annoying, so I keep an up to date compiled copy in dist/.

  1. Download the closure library
svn checkout http://closure-library.googlecode.com/svn/trunk/ closure-library
cd closure-library
  1. Download the closure compiler
curl http://closure-compiler.googlecode.com/files/compiler-latest.zip > compiler-latest.zip
unzip compiler-latest.zip compiler.jar
  1. Patch the library
patch -p0 <../node-browserchannel/closure-*.patch
  1. Build
cd ../node-browserchannel
make

License

Licensed under the standard MIT license:

Copyright 2011 Joseph Gentle.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.