RestWatch

Attempts to use WebSocket to receive asynchronous updates of changes in responses to REST calls. If WebSocket is not available, it falls back to polling REST requests to the server every 10 seconds. For asynchronous WebSocket support, it relies on the server side supporting web sockets and supporting the interaction protocol used by RestWatch.

var requestId = RestWatch.start('/rest/index/resources', params,
  this._onResponse);

WebSocket messages sent to the server are JSON and look like this:

{
  id: <id string>,
  url: <url string>,
  params: <params object>
}

The server should respond with JSON messages that look like this:

{
  id: <id string>,
  result: <result object>
}

When messages are received from the server, theresult will be sent to the callback that was passed to start().

RestWatch is resilient to the server restarting. If the connection is lost, RestWatch will poll every five seconds trying to re-establish the connection. When the connection is restored, all active watching is automatically resumed.

Methods

initialize
Initiate a connection to the server. This is optional as the start() method will perform this if needed.
start (url, params, function (result) )
Start watching the response to the REST request defined by the specified url and parameters. When updates are received, the handler function is called with the data returned from the server. This returns an opaque requestId object that must be used for corresponding calls to stop().
stop (requestId)
Stop watching the response to the REST request defined by the specified url and parameters.

Example

var Component = React.createClass({
  ...
  _onUpdate: function (result) {
    this.setState({result: result});
  },
  _getData: function () {
    this._request = RestWatch.start('/rest/index/resources',
      this.state.options.params, this._onUpdate);
  },
  ...
});