0.3.79 • Published 4 years ago

node-dataengine-client v0.3.79

Weekly downloads
1
License
MIT
Repository
bitbucket
Last release
4 years ago

Install

npm install node-dataengine-client

DataEngine

const DataEngine = require('node-dataengine-client').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

new DataEngine(options);

DataEngine Options

PropertyTypeDefaultDescription
hoststring127.0.0.1Data Engine Server host or ip
portnumber4300Data Engine Server port
pathstringPath to api if behind proxy
apiKeystringSecret key to access API (if set in server)
securebooleanfalseUse TLS
enablePingbooleanfalse
pingIntervalnumber5000Time in milliseconds
clientIdstringName of client, visible in admin UI status section
bucketstringDefault 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)

PropertyTypeDefaultDescription
bucketstringnullOptionally override default bucket
forceHttpbooleanfalseForce request to be sent over REST api
asyncbooleantrue

Example

const de = new DataEngine({ bucket: 'myBucket' });
de.read('myKey')
  .then(value => console.log(value));

list(opts)

List objects in bucket. Returns promise with array.

options (optional)

PropertyTypeDefaultDescription
bucketstringnullOptionally override default bucket
keysOnlybooleantrueSet to false to retrieve object content
forceHttpbooleanfalseForce request to be sent over REST api
asyncbooleantrue

Example

const de = new DataEngine({ bucket: 'myBucket' });
de.list()
  .then(objects => console.log(objects));

write(key, data, opts)

Returns promise

options (optional)

PropertyTypeDefaultDescription
bucketstringnullOptionally override default bucket
forceHttpbooleanfalseForce request to be sent over REST api
asyncbooleantrue
failIfExistsbooleanfalseOperation will fail if key already exists in data store
expirestringSet expiration for object, possible values: 'session' - Delete object when client disconnects

Example

const de = new DataEngine({ bucket: 'myBucket' });
de.write('myKey', { text: 'hello!' })
  .then(() => console.log('Written!');

patch(key, patch, opts)

Returns promise. JSON patch existing object.

options (optional)

PropertyTypeDefaultDescription
bucketstringnullOptionally override default bucket
removeDataFromReplybooleanfalseOptionally remove patch result from reply
forceHttpbooleanfalseForce request to be sent over REST api
asyncbooleantrue

Example

const de = new DataEngine({ bucket: 'myBucket' });
de.patch('myKey', [{ op: 'replace', path: '/text', value: 'hey!' }])
  .then(() => console.log('Patched!');

remove(key, opts)

Returns promise. Removes an object from data store.

options (optional)

PropertyTypeDefaultDescription
bucketstringnullOptionally override default bucket
forceHttpbooleanfalseForce request to be sent over REST api
asyncbooleantrue

Example

const de = new DataEngine({ bucket: 'myBucket' });
de.remove('myKey')
  .then(() => console.log('Removed!');

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

PropertyTypeDescription
bucketstringbucket of event origin
keystringkey of event origin
valueobjectwritten object if sendEvent was true in addListener call

Example

const de = new DataEngine({ bucket: 'myBucket' });
de.connection.addListener('myBucket', 'myKey', true, (event) => {
  console.log(`Event received for 
    key ${event.key} in
    bucket ${event.bucket},
    value=${event.value}`); 
});

signalEvent(bucket, key, value)

Send data to Data Engine only to be propagated as an event to other clients.

Example

const de = new DataEngine({ bucket: 'myBucket' });
de.connection.signalEvent(
  'myEventBucket',
  'myEventKey',
  { message: 'hello yall!' }
});

Events (callbacks)

onConnect(persistentConnection)

Callback triggered when connection is established.

Example

const de = new DataEngine({ bucket: 'myBucket' });
de.connection.onConnect = (serverId) => {
  console.log(`Client connected to server with id ${serverId}`);
};

onDisconnect()

Callback triggered when connection is closed.

Example

const de = new DataEngine({ bucket: 'myBucket' });
de.connection.onDisconnect = () => {
  console.log('Client disconnected');
};

onPong()

Callback triggered when pong packet is received from server.

Example

const de = new DataEngine({ bucket: 'myBucket' });
de.connection.onPong = () => {
  console.log('Server is responding!');
};

Messaging.RequestReply

const RequestReply = require('node-dataengine-client').Messaging.RequestReply;

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

new RequestReply.Requestor(DataEngineInstance, channel, timeoutInMs, progressTimeoutInMs);

Methods

request(msg)

Returns promise with reply.

Example

const de = new DataEngine();

// We cannot send request before we are connected
de.connection.onConnect = (serverId) => {
  // fail if response > 30 sec or if progress is not received < every 3 sec
  const requestor = new Requestor(de, 'my-channel', 30000, 3000);
  requestor.request({ msg: 'hello?' })
  .then(reply => console.log(`Got reply! ${reply}`);
  .catch(err => console.error(err));
};

Replier

Replier is used to receive and reply to messages.

Constructor

new RequestReply.Replier(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 = new DataEngine();

const replier = new Replier(de, 'my-channel');
replier.onMessage = (msg, reply, progress) => {
  console.log(`Got request: ${msg}`);
  // send progress after 2 sec, and reply after 5 sec
  setTimeout(() => progress({ someprop: 'some value' }), 2000);
  setTimeout(() => reply(null, { someProperty: 'some value' }), 5000);
};

Messaging.CompetingConsumers

const CompetingConsumers = require('node-dataengine-client').Messaging.CompetingConsumers;

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

new CompetingConsumers.Producer(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 = new DataEngine();

const producer = new Producer(de, 'render-jobs', 10000);
de.connection.onConnect = (serverId) => {
  producer.addJob({ someProperty: 'value' }, (progress) => console.log(progress))
  .then((data) => {
    console.log(`Job done: ${data.result}`);
  })
  .catch(err => console.error(err));
}

CompetingConsumer

Consumer of jobs.

Constructor

new CompetingConsumers.CompetingConsumer(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:

class Worker {
  constructor(id) {
    this.id = id;
    this.available = true;
  }

  work(job, done, progress) {
    // do things
    // signal progress with progress();
    done(err, result);
  }
  
  release() {
    this.available = true;
  }
}

Example

class MyWorker {
  constructor(id) {
  this.id = id;
  this.available = true;
  }

  work(job, done, progress) {
    console.log('working..');
    // send progress every sec
    setTimeout(() => progress({ someProperty: 'value' }), 1000);
    // signal done after 10 sec
    setTimeout(() => done(null, { someProperty: 'value' }), 10000);
  }
  
  release() {
    this.available = true;
  }
}

const de = new DataEngine();

const consumer = new CompetingConsumer(de, 'my-channel');
const worker = new MyWorker('myworker1');

consumer.onJobQuery = (job, callback) => {
  if (worker.available) {
    callback(null, worker);
  } else {
    callback(200); // no worker available
  }
};
0.3.79

4 years ago

0.3.78

5 years ago

0.3.77

5 years ago

0.3.76

5 years ago

0.3.75

5 years ago

0.3.74

5 years ago

0.3.73

6 years ago

0.3.72

6 years ago

0.3.71

6 years ago

0.3.70

6 years ago

0.3.69

6 years ago

0.3.68

6 years ago

0.3.67

6 years ago

0.3.66

6 years ago

0.3.65

6 years ago

0.3.64

6 years ago

0.3.63

6 years ago

0.3.62

6 years ago

0.3.61

6 years ago

0.3.6

6 years ago

0.3.5

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.92

6 years ago

0.2.91

7 years ago

0.2.90

7 years ago

0.2.89

7 years ago

0.2.88

7 years ago

0.2.87

7 years ago

0.2.86

7 years ago

0.2.85

7 years ago

0.2.84

7 years ago

0.2.83

7 years ago

0.2.82

7 years ago

0.2.81

7 years ago

0.2.8

7 years ago

0.2.7

7 years ago

0.2.61

7 years ago

0.2.5

7 years ago

0.2.4

7 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.8

8 years ago

0.1.7

8 years ago

0.1.6

8 years ago

0.1.5

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.6

8 years ago