WebChan.nl

A channel service providing server-push capabilities to web developers.

Enable push events on every website by calling just 4 JavaScript functions.

Add the following line to your HTML file:

<script src="https://webchan.nl/channel.js"></script>

Now you're ready to read from the channel:

Channel.init(function () {
        var source = Channel.eventSource();
        source.addEventListener("command1", command1Handler);
        source.addEventListener("command2", command2Handler);
        source.addEventListener("command3", command3Handler);
    });

This is how you post data to the channel:

curl -d "name=command1&data=eventdata" https://webchan.nl/channel/CHANNEL_ID/post

See it in action!

JS API reference

After including channel.js you get a Channel object populating four functions. Here's how they work.

Channel.init(callback)

Calls the channel server and reserves a channel for you. After a channel has been acquired callback is called without arguments.

Channel.channel_id()

Returns the ID of the channel you've acquired with Channel.init(callback). This unique ID identifies your channel, everyone knowing it can post to the channel so you should keep it secret as long as you're connected.

Channel.eventSource()

Notice that cross-domain EventSource objects are only implemented in Firefox at the moment. A polyfill exists but I've not integrated it into the JS API, yet.

Returns an EventSource object. After acquiring a channel you can immediately start listening for events. The EventSource object returned by this function is already connect to the channel. For a reference of this object click here.

Channel.qrcode(url, callback)

Generates a QR code pointing to url. The QR code is generated on the WebChan.nl server and the result passed to the callback function as base64 encoded PNG image. You can easily embed it by setting the src attribute of an img tag like this: data:image/png;base64,QRCODE. Just exchange QRCODE with the data passed to your callback function.

REST API reference

If you don't want to use the JavaScript API you can easily just use GET and POST requests to the REST API endpoints. Using this API you can establish a server-to-server channel, for example.

https://webchan.nl/channel/acquire

Method: GET

Parameters: -

Returns: STRING

Reserves a channel and returns its ID. Keep this ID secret since everyone knowing it can read and write events from/to the channel.

https://webchan.nl/channel/ID

Method: GET

Parameters: ID

Returns: events

When you request this URL you've opened the channel identified by ID. The server will block the request forever. When new events are posted to the channel they are returned in the following format:

event: EVENTNAME
data: DATA
                    
The event is terminated by an empty line. EVENTNAME and DATA are explained in the next call.

https://webchan.nl/channel/ID/post

Method: POST

Parameters: ID

POST data: name=NAME&data=DATA

Returns: {"status": "ok"}

POSTing to this URL will result in an event fired on the channel identified by ID. NAME and DATA can be arbitrary strings of your choice. The only constraint is that your receiver listening on the channel is able to handle the event. When you POST to a non-existant channel an HTTP status of 404 is returned.

https://webchan.nl/channel/ID/qrcode?base_url=URL

Method: GET

Parameters: ID, URL

Returns: base64-encoded image

Generates a QR code pointing to URL. This QR code is exceptionally handy if you want to establish a local browser-to-browser connection (e.g. to control a slide show with your smartphone).