Install
npm install node-dataengine-client
DataEngine
const DataEngine = DataEngine;
Communicates with Data Engine, maintains a persistent connection, handles data store operations and event signaling. If the persistent connection (websocket) is not connected, REST will be used. Events can be registered without having the connection active, the client will automatically register them with the server when connection is established. Works with node.js and in browser.
Constructor
options;
DataEngine Options
Property | Type | Default | Description |
---|---|---|---|
host | string | 127.0.0.1 | Data Engine Server host or ip |
port | number | 4300 | Data Engine Server port |
path | string | Path to api if behind proxy | |
apiKey | string | Secret key to access API (if set in server) | |
secure | boolean | false | Use TLS |
enablePing | boolean | false | |
pingInterval | number | 5000 | Time in milliseconds |
clientId | string | Name of client, visible in admin UI status section | |
bucket | string | Default bucket for operations |
Methods
read(key, [opts])
Returns promise with read object. If empty string is passed as key, an array of all objects in bucket will be returned.
options (optional)
Property | Type | Default | Description |
---|---|---|---|
bucket | string | null | Optionally override default bucket |
forceHttp | boolean | false | Force request to be sent over REST api |
async | boolean | true |
Example
const de = bucket: 'myBucket' ;de ;
list([opts])
List objects in bucket. Returns promise with array.
options (optional)
Property | Type | Default | Description |
---|---|---|---|
bucket | string | null | Optionally override default bucket |
keysOnly | boolean | true | Set to false to retrieve object content |
forceHttp | boolean | false | Force request to be sent over REST api |
async | boolean | true |
Example
const de = bucket: 'myBucket' ;de ;
write(key, data, [opts])
Returns promise
options (optional)
Property | Type | Default | Description |
---|---|---|---|
bucket | string | null | Optionally override default bucket |
forceHttp | boolean | false | Force request to be sent over REST api |
async | boolean | true | |
failIfExists | boolean | false | Operation will fail if key already exists in data store |
expire | string | Set expiration for object, possible values: 'session' - Delete object when client disconnects |
Example
const de = bucket: 'myBucket' ;de
patch(key, patch, [opts])
Returns promise. JSON patch existing object.
options (optional)
Property | Type | Default | Description |
---|---|---|---|
bucket | string | null | Optionally override default bucket |
removeDataFromReply | boolean | false | Optionally remove patch result from reply |
forceHttp | boolean | false | Force request to be sent over REST api |
async | boolean | true |
Example
const de = bucket: 'myBucket' ;de
remove(key, [opts])
Returns promise. Removes an object from data store.
options (optional)
Property | Type | Default | Description |
---|---|---|---|
bucket | string | null | Optionally override default bucket |
forceHttp | boolean | false | Force request to be sent over REST api |
async | boolean | true |
Example
const de = bucket: 'myBucket' ;de
Properties
Connection: PersistentConnection
PersistentConnection
PersistentConnection is automatically created in the DataEngine instance as property connection
Methods
addListener(bucket, key, sendValues, callbackFn(eventObject))
Add listener for specific bucket and key. Wildcard * can be used to listen to all events in a bucket. If * is used for bucket, events will be triggered for all buckets.
eventObject
Property | Type | Description |
---|---|---|
bucket | string | bucket of event origin |
key | string | key of event origin |
value | object | written object if sendEvent was true in addListener call |
Example
const de = bucket: 'myBucket' ;deconnection;
signalEvent(bucket, key, value)
Send data to Data Engine only to be propagated as an event to other clients.
Example
const de = bucket: 'myBucket' ;deconnection;
Events (callbacks)
onConnect(persistentConnection)
Callback triggered when connection is established.
Example
const de = bucket: 'myBucket' ;deconnection { console;};
onDisconnect()
Callback triggered when connection is closed.
Example
const de = bucket: 'myBucket' ;deconnection { console;};
onPong()
Callback triggered when pong packet is received from server.
Example
const de = bucket: 'myBucket' ;deconnection { console;};
Messaging.RequestReply
const RequestReply = MessagingRequestReply;
Thin abstraction for sending and receiving messages between two clients connected to same Data Engine. Data Engine messaging is using the signalEvent method of PersistentConnection.
Requestor
Requestor is used to send messages to a receiver. Request will fail if no response is received within timeoutInMs or if no progress update is received within progressTimeoutInMs.
Constructor
DataEngineInstance channel timeoutInMs progressTimeoutInMs;
Methods
request(msg)
Returns promise with reply.
Example
const de = ; // We cannot send request before we are connecteddeconnection { // fail if response > 30 sec or if progress is not received < every 3 sec const requestor = de 'my-channel' 30000 3000; requestor
Replier
Replier is used to receive and reply to messages.
Constructor
DataEngineInstance channel;
Events
onMessage(msg, replyFn(err, obj), progressFn(obj))
Called when a message is received. Reply is sent by calling replyFn with either an error or some data. In case of lengthy processing before reply can be sent, replier can call progressFn to avoid timeout in requestor.
Example
const de = ; const replier = de 'my-channel';replier { console; // send progress after 2 sec, and reply after 5 sec ; ;};
Messaging.CompetingConsumers
const CompetingConsumers = MessagingCompetingConsumers;
This pattern is used when one producer generates jobs that many listening consumers can claim. Only one of the consumers can claim each job.
Producer
Producer creates jobs.
Constructor
DataEngineInstance channel timeoutInMs progressTimeoutInMs;
Methods
addJob(job, progressFn?, timoutInMs?, progressTimeoutInMs?)
Returns promise, resolved if consumer processed it successfully. Consumer can optionally call a progress callback (passed in progressFn) during its job processing.
Example
const de = ; const producer = de 'render-jobs' 10000;deconnection { producer ;}
CompetingConsumer
Consumer of jobs.
Constructor
DataEngineInstance channel;
Events
onJobQuery(job, callback(errno, worker))
Called when a job is requested by producer. errno should have one of values:
const JOB_NOT_SUPPORTED = 100;const NO_WORKER_AVAILABLE = 200;const JOB_OK = null;
Worker object must implement following:
{ thisid = id; thisavailable = true; } { // do things // signal progress with progress(); ; } { thisavailable = true; }
Example
{ thisid = id; thisavailable = true; } { console; // send progress every sec ; // signal done after 10 sec ; } { thisavailable = true; } const de = ; const consumer = de 'my-channel';const worker = 'myworker1'; consumer { if workeravailable ; else ; // no worker available };