<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>" />
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.
See the Infinispan Server page for details.
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>" />
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... </script>
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...
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... cache.notify("orderStatus"); cache.notify("expectedDeliveryTime"); // Register the callback function for receiving notifcations... cache.registerCallback(cacheCallback); // 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)" /> </form>
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.
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.
Prototype/Alpha.