Skip to content

Commit

Permalink
Throw error on websocket.onerror event
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Stumpf committed Jun 13, 2019
1 parent 88f14af commit 3647c58
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,30 @@ For more information on access tokens, see the API's [Authentication Documentati

### SignalFlow

#### Configuring the Signalflow websocket endpoint

If the websocket endpoint is not set manually, this library uses the ``us0`` realm by default.
If you are not in this realm, you will need to explicitly set the
endpoint urls above. To determine if you are in a different realm and need to
explicitly set the endpoints, check your profile page in the SignalFx
web application.


#### Examples

Complete code example for executing a computation
```js
var signalfx = require('signalfx');

var wsCallback = function(evt) {
console.log('Hello, I'm a custom callback: ' + evt);
}
var myToken = '[ACCESS_TOKEN]';
var options = {'signalflowEndpoint': 'wss://stream.{REALM}.signalfx.com'};
var options = {'signalflowEndpoint': 'wss://stream.{REALM}.signalfx.com',
'apiEndpoint': 'https://api.{REALM}.signalfx.com',
'webSocketErrorCallback': wsCallback
};

var client = new signalfx.SignalFlow(myToken, options);

Expand All @@ -240,7 +256,13 @@ var handle = client.execute({
handle.stream(function(err, data) { console.log(data); });
```
Please note that a token created via the REST API is necessary to use this API. API Access tokens intended for ingest are not allowed.
Object `options` is an optional map and may contains following fields:
+ **signalflowEndpoint** - string, `wss://stream.us0.signalfx.com` by default. Override this if you are in a different realm than `us0`.
+ **apiEndpoint** - string, `https://api.us0.signalfx.com` by default. Override this if you are in a different realm than `us0`.
+ **webSockerErrorCallback** - function, Throws an Error event by default. Override this if you want to handle a websocket error differently.

**Note**: A token created via the REST API is necessary to use this API. API Access tokens intended for ingest are not allowed.


#### API Options

Expand Down
21 changes: 19 additions & 2 deletions lib/client/signalflow/request_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,33 @@ var conf = require('../conf');
var SSE = require('sse.js');


function RequestManager(endPoint) {
function RequestManager(options) {
var awaitingKeepaliveConfirmation = false;
var knownComputations = {};
var requestIdIndex = 0;
var maxJobCount = 10000;
var authToken = null;
var sfxEndpoint = conf.DEFAULT_API_ENDPOINT; // TODO : SSE support?
var wsEndpoint = endPoint || conf.DEFAULT_SIGNALFLOW_WEBSOCKET_ENDPOINT;
var wsEndpoint = conf.DEFAULT_SIGNALFLOW_WEBSOCKET_ENDPOINT;
var pendingWSRequests = [];
var wsConnectionOpen = false;

var socketError = function (evt) {
throw new Error('Socket error: ' + evt);
};

if (options && options.signalflowEndpoint) {
wsEndpoint = options.signalflowEndpoint;
}

if (options && options.apiEndpoint) {
sfxEndpoint = options.apiEndpoint;
}

if (options && options.webSocketErrorCallback && options.webSocketErrorCallback instanceof Function) {
socketError = options.webSocketErrorCallback;
}

//move me to constants
var transports = {
WEBSOCKET: 1,
Expand Down Expand Up @@ -154,6 +170,7 @@ function RequestManager(endPoint) {
activeSocket = new WebSocket(wsEndpoint + '/v2/signalflow/connect');
activeSocket.binaryType = 'arraybuffer';
activeSocket.onmessage = onWebSocketMessage;
activeSocket.onerror = socketError;
activeSocket.onopen = function (ev) {
activeSocket.send(JSON.stringify({type: 'authenticate', token: authToken}));
wsConnectionOpen = true;
Expand Down
6 changes: 1 addition & 5 deletions lib/client/signalflow/signalflow_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ var RequestManager = require('./request_manager');


function SignalflowClient(apiToken, options) {
var signalflowEndpoint = null;
if (options && options.signalflowEndpoint) {
signalflowEndpoint = options.signalflowEndpoint;
}
var rm = new RequestManager(signalflowEndpoint);
var rm = new RequestManager(options);

rm.authenticate(apiToken);

Expand Down

0 comments on commit 3647c58

Please sign in to comment.