1.0.5 • Published 5 years ago

@openstfoundation/openst-notification v1.0.5

Weekly downloads
2
License
LGPL-3.0
Repository
github
Last release
5 years ago

OpenST Notification

Latest version Travis Downloads per month Gitter

OpenST Notification helps publish critical events from OpenST Platform and other related packages. All events get published using node EventEmitter and, if configured, events are also published through RabbitMQ, using topic-based exchange.

Install OpenST Notification

npm install @openstfoundation/openst-notification --save

Set ENV Variables

To configure OpenST Notification for use with RabbitMQ, set the following environment variables:

export OST_RMQ_SUPPORT='1' # Possible values are - '0' (disable), '1' (enable)
export OST_RMQ_HOST='127.0.0.1'
export OST_RMQ_PORT='5672'
export OST_RMQ_USERNAME='guest' # Default RabbitMQ user name
export OST_RMQ_PASSWORD='guest' # Default RabbitMQ password
export OST_RMQ_HEARTBEATS='30'

Examples:

Subscribe to OpenST events published through RabbitMQ:

  • Basic example on how to listen a specific event. Arguments passed are:

    • Events Array - List of events to subscribe to
    • Options object -
      • queue string - Name of the queue on which you want to receive all your subscribed events. These queues and events, published in them, have TTL of 6 days. If a queue name is not passed, a queue with a unique name is created and is deleted when the subscriber gets disconnected.
      • ackRequired number - (optional) - The delivered message needs ack if passed 1 ( default 0 ). if 1 passed and ack not done, message will redeliver.
      • prefetch number - The number of messages released from queue in parallel. In case of ackRequired=1, queue will pause unless delivered messages are acknowledged.
    • Callback function - Callback method will be invoked whenever there is a new notification
const openSTNotification = require('@openstfoundation/openst-notification');

var unAckCount = 0; //Number of unacknowledged messages.

openSTNotification.subscribeEvent.rabbit(
  ["event.ProposedBrandedToken"],
  {
    queue: 'myQueue',
    ackRequired: 1, // When set to 1, all delivered messages MUST get acknowledge. 
    prefetch:10
  }, 
  function(msgContent){
    // Please make sure to return promise in callback function. 
    // On resolving the promise, the message will get acknowledged.
    // On rejecting the promise, the message will be re-queued (noAck)
    return new Promise(async function(onResolve, onReject) {
      // Incrementing unacknowledged message count.
      unAckCount++;
      console.log('Consumed message -> ', msgContent);
      response = await processMessage(msgContent);
      
      // Complete the task and in the end of all tasks done



      if(response == success){
        // The message MUST be acknowledged here.
        // To acknowledge the message, call onResolve
        // Decrementing unacknowledged message count.
        unAckCount--;
        onResolve();   
      } else {
        //in case of failure to requeue same message.
        onReject();
      }
     
    })
  
  });
  
// Gracefully handle SIGINT, SIGTERM signals.
// Once SIGINT/SIGTERM signal is received, programme will stop consuming new messages. 
// But, the current process MUST handle unacknowledged queued messages.
process.on('SIGINT', function () {
  console.log('Received SIGINT, checking unAckCount.');
  var f = function(){
    if (unAckCount === 0) {
      process.exit(1);
    } else {
      console.log('waiting for open tasks to be done.');
      setTimeout(f, 1000);
    }
  };

  setTimeout(f, 1000);
});
  • Example on how to listen to multiple events with one subscriber.
const openSTNotification = require('@openstfoundation/openst-notification');
openSTNotification.subscribeEvent.rabbit(
  ["event.ProposedBrandedToken", "obBoarding.registerBrandedToken"],
  {}, 
  function(msgContent){
    console.log('Consumed message -> ', msgContent)
  });

Subscribe to OpenST local events published through EventEmitter:

  • Basic example on how to listen a specific event. Arguments passed are:

    • Events (mandatory) - List of events to subscribe to
    • Callback (mandatory) - Callback method will be invoked whenever there is a new notification
const openSTNotification = require('@openstfoundation/openst-notification');
openSTNotification.subscribeEvent.local(["event.ProposedBrandedToken"], function(msgContent){console.log('Consumed message -> ', msgContent)});

Publish to OpenST Notifications:

  • All events are by default published using EventEmitter and if configured, through RabbmitMQ as well.
const openSTNotification = require('@openstfoundation/openst-notification');
openSTNotification.publishEvent.perform(
  {
    topics:["event.ProposedBrandedToken"], 
    publisher: 'MyPublisher',
    message: {
	  kind: "event_received",
	  payload: {
		event_name: 'ProposedBrandedToken',
		params: {
		  //params of the event
		},
        contract_address: 'contract address',
        chain_id: 'Chain id',
        chain_kind: 'kind of the chain'
	  }
	}
  });

For further implementation details, please refer to the API documentation.

1.0.6-beta.10

5 years ago

1.0.6-beta.9

5 years ago

1.0.6-beta.8

5 years ago

1.0.6-beta.7

5 years ago

1.0.6-beta.6

5 years ago

1.0.6-beta.5

5 years ago

1.0.6-beta.4

5 years ago

1.0.6-beta.3

5 years ago

1.0.6-beta.2

5 years ago

1.0.5-beta.1

6 years ago

1.0.6-beta.1

6 years ago

1.0.5

6 years ago

1.0.4-beta.10

6 years ago

1.0.4-beta.9

6 years ago

1.0.4-beta.8

6 years ago

1.0.4-beta.7

6 years ago

1.0.4-beta.6

6 years ago

1.0.4-beta.5

6 years ago

1.0.4-beta.4

6 years ago

1.0.3

6 years ago

1.0.4-beta.3

6 years ago

1.0.4-beta.2

6 years ago

1.0.4-beta.1

6 years ago

1.0.3-beta.6

6 years ago

1.0.3-beta.5

6 years ago

1.0.3-beta.4

6 years ago

1.0.3-beta.3

6 years ago

1.0.3-beta.2

6 years ago

1.0.3-beta.1

6 years ago

1.0.2

6 years ago

1.0.2-beta.2

6 years ago

1.0.2-beta.1

6 years ago

1.0.1

6 years ago

1.0.1-beta.3

6 years ago

1.0.1-beta.2

6 years ago

1.0.1-beta.1

6 years ago

1.0.0

6 years ago

1.0.0-beta.13

6 years ago

1.0.0-beta.12

6 years ago

1.0.0-beta.11

6 years ago

1.0.0-beta.10

6 years ago

1.0.0-beta.9

6 years ago

1.0.0-beta.8

6 years ago

1.0.0-beta.7

6 years ago

1.0.0-beta.6

6 years ago

1.0.0-beta.5

6 years ago

1.0.0-beta.4

6 years ago

1.0.0-beta.3

6 years ago

1.0.0-beta.2

6 years ago

1.0.0-beta.1

6 years ago

0.9.0-beta.1

6 years ago