JBoss Community Archive (Read Only)

Infinispan 6.0

WebSocket Server

The Infinispan WebSocket Server can be used to expose an Infinispan Cache instance over a WebSocket Interface via a very simple Javascript "Cache" API. The WebSocket Interface was introduced as part of the HTML 5 specification.  It defines a full-duplex communication channel to the browser, operating over a single socket (unlike Comet or Ajax) and is exposed to the browser via a Javascript interface.

Starting The Server

See the Infinispan Server page for details.

Javascript API

Writing a web page that uses the Infinispan Cache API is trivial.  The page simply needs to include a <script> declaration for the infinispan-ws.js Javascript source file.  This script is served up by WebSocket Server.

So, for loading infinispan-ws.js from a WebSocket Server instance running on www.acme.com:8181 (default port):

<script type="text/javascript" src="<a href="http://www.acme.com:61999/infinispan-ws.js" target="_blank">http://www.acme.com:8181/infinispan-ws.js</a>" />

Creating a Client-Side Cache Object Instance

The client-side interface to a server-side Infinispan cache is the Cache Javascript object.  It can be constructed as follows:

<script type="text/javascript">
    var cache = new Cache();
    // etc...
By default, the Cache instance will interface to the default Infinispan Cache associated with the WebSocket Server from which the infinispan-ws.js Javascript source file was loaded.  So, in the above case, the Cache object instance will connect to the WebSocket Server running on www.acme.com:8181 (i.e. ws://www.acme.com:8181).

The Infinispan Cache name and WebSocket Server address can be specified in the {{Cache} object constructor as follows:

var cache = new Cache("omCache", "ws://ws.acmews.com:8181");
// etc...

Cache Operations

A number of cache operations can be performed via the Cache object instance such as get, put, remove, notify and unnotify.

The get and notify operations require a callback function to be registered with the Cache object instance.  This callback function receives all add/update/remove notifications on any cache entries for which the notify function was invoked.  It also asynchronously receives the result of a single invocation of the get function i.e. get can be thought of as "notify once, immediately".

The callback function is registered with the Cache object instance via the registerCallback function.  The function should have 2 parameters - key and value, relating to the cache key and value.

var cache = new Cache();

// Ask to be notified about some cache entries...

// Register the callback function for receiving notifcations...

// Cache callback function...
function cacheCallback(key, value) {
    // Handle notification...

Getting and updating data in the cache is done by simply calling the get, put and remove functions on the Cache object instance.  These operations could be triggered by user interaction with a web form e.g.

<form onsubmit="return false;">

    <!-- Other form components... -->

    <!-- Buttons for making cache updates... -->
    <input type="button" value="Put"
           onclick="cache.put(this.form.key.value, this.form.val.value)" />
    <input type="button" value="Get"
           onclick="cache.get(this.form.key.value)" />
    <input type="button" value="Remove"
           onclick="cache.remove(this.form.key.value)" />

Sample code

Infinispan's source tree contains a sample HTML document that makes use of the WebSocket server.  Browse through the source of this HTML document here.

Browser Support

At the time of writing, Google Chrome was the only browser with native WebSocket support.  However, the jWebSocket project provides a client side Javascript library that adds WebSocket support to any Flash enabled browser.


See the following demo of the Infinispan WebSocket Server in action.



JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-11 09:40:30 UTC, last content change 2013-07-08 14:18:19 UTC.