HTML5

Basic R Websockets Example


               
Connection not initialized


The websockets package is an HTML 5 Websocket implementation for the R language, based on the libwebsockets library written by Andy Green. The websockets package is especially well-suited to lightweight interaction between R and web scripting languages like Javascript. Multiple simultaneous websocket connections are supported.

By 'lightweight' we mean that the library has few external dependencies and is easily portable. More significantly, websockets lets Javascript and other scripts embedded in web pages directly interact with R, bypassing traditional middleware layers like .NET, Java, and web servers normally used for such interaction.

The HTML 5 Websocket API is a modern socket-like communication protocol for the web. Note that the HTML 5 Websocket API is still under development and may change. Some browsers may not enable Websockets by default (including recent versions of Firefox), but there are usually simple methods to enable the API. Despite its developmental status, the API is presently widely supported: most recent browsers support it and there are many available langauge implementations.

This simple example illustrates a few features of websockets. For a fancier demo that uses JSON, run the following in your R session:

demo('json', package='websockets')
The features illustrated in this example are:


websockets package Copyright (C) 2011 by Bryan W. Lewis, <blewis@illposed.net>, package licensed under GNU GPL v2.
libwebsockets Copyright (C) 2010 by Andy Green <andy@warmcat.com>, licensed under GNU LGPL v2.1.
BSD poll library Copyright (C) 1995-2002 by Brian M. Clapper <bmc@clapper.org>, licensed under BSD license.