jsish
WebSocket
Not logged in

WebSocket Javascript Interface

The WebSocket extension uses libwebsockets to implement bidirectional socket communication with a web browser.

When used in conjunction with Sqlite and JSON, it is a simple matter to implement browser based applications.

Example

The following creates a minimal client and server using WebSockets.

First the server file ws.js:

function ws_input(data, id) {
    puts("ws_input: " + id + ": " + data);
};

var ws = new WebSocket({callback:ws_input});
var msg = { str:"whos there?", cnt:0 };
while (true) {
    update(1);
    if ((msg.cnt++ % 10) == 0)
       ws.send(JSON.stringify(msg));
}

Next the client file: wsc.js:

function wsc_input(data) {
    puts("wsc_input: " + data);
};

var ws = new WebSocket({client:true, callback:wsc_input});
var msg = { str:"knock knock", cnt:0 };

while (true) {
    msg.cnt++;
    ws.send(JSON.stringify(msg));
    update(1);
}

Which we run with:

jsish ws.js &
jsish wsc.js

For more concrete working examples, see the example apps.