Contents
Introduction
This class is used to interact with evQueue core engine using Websockets in an event driven way.
Also the evQueue class can we used directly, we recommand using the evQueueCluster instead which is more simple to use.
Source code can be found in our sample Websockets project on GitHub.
Credentials
One major drawback of this approach is that credentials must be stored client side. To avoid storing clear text password, we store hashed (SHA1) password in local storage. This can me achieved with the following code :
import {CryptoJS} from './cryptojs/core.js'; let user = 'admin'; let password = CryptoJS.SHA1('admin').toString(CryptoJS.enc.Hex); // Store crendentials in local storage window.localStorage.setItem('user',user); window.localStorage.setItem('password',password);
If your application uses identification, you can store the hashed password when the client identifies, this avoids having clear text password in the javascript source.
Be aware that anyway, this implies evQueue will be world accessible, so this is really not recommanded for external applications.
Accessing the API
Using websockets you can still access traditional API :
let evqueue_api = new evQueueCluster(['ws://localhost:5001']); evqueue_api.API({ group: 'instances', action: 'list' });
This returns a Javascript promise with the result of the API command.
Using events
Using events works the same way but you must add a callback function that will be called when events occur :
let evqueue_api = new evQueueCluster(['ws://localhost:5001'], (data) => { console.log(data); });
Events are unidirectionnal (from evQueue to Javascript). You have to subscribe before receiving events (see below).
Subscribing events
Event subscription is made with the Subscribe() method :
Subscribe(event,api,send_now,object_id,external_id)
Event is the name of an event (see Events API).
api is an API command (see API) described as an object containing the following attributes : group, action, attributes and node. Group and action are the API command group and action. Attributes are the command parameters. Node can we used if you want to subscribe a specific node (* is used to subscribe on all nodes).
send_now is a boolean used to get an immediate result of the API command, even if no event occurs (this is useful for initialization).
object_id is used to filter only events on a specific object (like an instance).
external_id is a numerical ID that will be sent back to you when this events triggers. This is useful in the callback to know which event has been triggered.
To list instances each time an instance is terminated (and get a first response for init purpose) :
evqueue_events.Subscribe('INSTANCE_TERMINATED', {group: 'instances', action: 'list'}, true);
Unsubscribing events
Unsubscription is made with :
Unsubscribe(event, external_id, object_id)
event is a type of event you have subscribed to.
external_id and object_id as used as optional filters to find which specific subscription you want to remove.
For some simple cases, you can simply use :
UnsubscribeAll()
Which will unsubscribe all events on the current connection.