Class: StreamdataEventSource

StreamdataEventSource

new StreamdataEventSource(url, token, headers, authStrategy)

The StreamdataEventSource object is the main entry point to establish a connection to a targeted JSON REST service URL.

Use streamdataio.createEventSource(url,token[,headers][,authStrategy]) to create new instance.

Parameters:
Name Type Description
url String Mandatory. The targeted REST URL is formatted as follow:
protocol://url(:port)(/localpath(?queryparameters))
token String Mandatory. The application token to authentify the request
headers Array Optional. Any specific headers that have to be added to the request. It must be an array with the following structure:['Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==']
authStrategy Object Optional. An object which will enable HMAC signature of the request. You can create this object as follow:

// setup headers
var headers = [];
// setup signatureStrategy
var signatureStrategy = AuthStrategy.newSignatureStrategy('NmEtYTljN2UtYmM1MGZlMGRiNGFQzYS00MGRkLTkNTZlMDY1','NTEtMTQxNiWIzMDEC00OWNlLThmNGYtY2ExMDJxO00NzNhLTgtZWY0MjOTc2YmUxODFiZDU1NmU0ZDAtYWU5NjYxMGYzNDdi');
// instantiate an eventSource
var eventSource = streamdataio.createEventSource('http://myRestservice.com/stocks','NmEtYTljN2UtYmM1MGZlMGRiNGFQzYS00MGRkLTkNTZlMDY1',headers,signatureStrategy);

Methods

close() → {StreamdataEventSource}

Close StreamdataEventSource connection.

The StreamdataEventSource will disconnect and close the connection.

Returns:
Type
StreamdataEventSource

isConnected() → {StreamdataEventSource}

check the connection status of the StreamdataEventSource.

Returns:
true if is connected, false otherwise.
Type
StreamdataEventSource
Example
if (eventSource.isConnected()) {
  // perform required action
};

onData(callback) → {StreamdataEventSource}

Register a callback called when the client application received initial data (snapshot) from the server.

Parameters:
Name Type Description
callback function the function to notify.
Properties
Name Type Description
data data the Json object sent by the server containing initial data (snapshot) in Json format.
Returns:
Type
StreamdataEventSource
Example
eventSource.onData(function(data) {
  // callback code
});

onError(callback) → {StreamdataEventSource}

Register a callback called when the client application experienced an error.

Parameters:
Name Type Description
callback function the function to notify.
Properties
Name Type Description
aError aError the StreamdataError received in case of error.
Returns:
Type
StreamdataEventSource
Example
eventSource.onError(function(aError) {
  // callback code
  // aError.getMessage();
});

onMonitor(callback) → {StreamdataEventSource}

Register a callback called when the client application received monitor data from the server.

Parameters:
Name Type Description
callback function the function to notify.
Properties
Name Type Description
monitor monitor the Json object sent by the server containing monitor data in Json format.
Returns:
Type
StreamdataEventSource
Example
eventSource.onMonitor(function(monitor) {
  // callback code
});

onOpen(callback) → {StreamdataEventSource}

Register a callback called when the client application opened successfully a StreamdataEventSource.

Parameters:
Name Type Description
callback function the function to call.
Returns:
Type
StreamdataEventSource
Example
eventSource.onOpen(function() {
  // callback code
});

onPatch(callback) → {StreamdataEventSource}

Register a callback called when the client application received incremental update data (patch) from the server.

Parameters:
Name Type Description
callback function the function to notify.
Properties
Name Type Description
patch patch the Json object sent by the server containing incremental update data (patch) in Json patch format.
Returns:
Type
StreamdataEventSource
Example
eventSource.onPatch(function(patch) {
  // callback code
});

open() → {StreamdataEventSource}

Open StreamdataEventSource connection to the URL.

The StreamdataEventSource will auto reconnect if the connection is lost until close() is called.

Returns:
Type
StreamdataEventSource