Giter Club home page Giter Club logo

defra.ffc-messaging's Introduction

ffc-messaging

Messaging npm module for FFC services

Usage

Installation

npm install --save ffc-messaging

Configuration

name - name of connection, if not supplied the address name will be used. This value is also used in App Insights tracing

host - Azure Service Bus namespace, for example, myservicebus.servicebus.windows.net

useCredentialChain - Boolean value for whether to authenticate connection with using Azure's credential chain. For example, set this to true if you wish to use AAD Pod Identity. If false, then username and password are required.

username - Azure Service Bus Shared Access Key name for authentication. Not required if useCredentialChain is true.

password - Azure Service Bus Shared Access Key value for authentication. Not required if useCredentialChain is true.

type - Azure Service Bus entity to connect to, allows queue, topic or subscription.

address - Name of the Azure Service Bus queue, topic or subscription to connect to.

topic - Required for subscription connections only. The name of the topic the subscription belongs to.

appInsights - Application Insights module if logging is required

retries - How many times should a sender try to send a message, defaulting to 5 if not supplied. With Pod Identity and Azure Identity there is a scenario that the identity will not be allocated in time for it's usage which causes failure sending messages. 5 is usually sufficient but can be increased if necessary.

retryWaitInMs - How long should a sender wait in milliseconds before trying to resend, defaulting to 500 if not supplied.

exponentialRetry - Whether to exponentially retry, ie doubling the retryWaitInMs on every retry. Defaulted to false.

autoCompleteMessages - (Subscriptions only - see below) Whether to auto complete messages once the action method has ran. Defaults to false.

Example

const config = {
  host: 'myservicebus.servicebus.windows.net',
  useCredentialChain: false,
  username: 'mySharedAccessKeyName',
  password: 'mySharedAccessKey,
  address: 'mySubscription,
  type: 'subscription',
  topic: 'myTopic',
  appInsights: require('applicationinsights'),
  retries: 5
}

Sending a message

Message objects must follow the below structure.

body - The body of the message.

type - Type of message using reverse DNS notation. For example, uk.gov.demo.claim.validated.

subject - Optional, if the body alone is not sufficient to give context to the recipient. For example, myImage.jpeg.

source - Name of the service sending the message. For example, ffc-demo-claim-service

correlationId - Optional, if distributed tracing through Application Insights is required.

Example

const message = {
  body: { claimId: 1 },
  type: 'uk.gov.demo.claim.validated',
  subject: 'New Claim',
  source: 'ffc-demo-claim-service'
}
const sender = new MessageSender(config)
await sender.sendMessage(message)

// shutdown when needed
await sender.closeConnection()

The sendMessage function can also receive all options applicable to Azure Service Bus sendMessages as a parameter, see Azure documentation.

await sender.sendMessage(message, options)

Receiving a message

There are multiple options for receiving a message.

Subscribe

Permanantely subscribe to all messages. Automatically will handle any intermittant disconnects.

const action = function (message) {
  console.log(message.body)
}

const receiver = new MessageReceiver(config, action)
await receiver.subscribe()

// shutdown when needed
await receiver.closeConnection()

Receive

Single call to receive current messages messages.

const receiver = new MessageReceiver(config, action)
// receive a maximum of 10 messages
messages = await receiver.receiveMessages(10)

// shutdown when needed
await receiver.closeConnection()

The receiveMessages function can also receive all options applicable to Azure Service Bus receiveMessages as a parameter, see Azure documentation.

await receiver.receiveMessages(10, options)

It is often beneficial when using this to specify the maximum wait time for both the first message and the last message to improve performance of the application. For example:

// This will wait a maximum of one second for the first message, if no message exists then the response will return.  
// If a message is received within one second it will wait a further five seconds or until it receives 10 messages to return
messages = await receiver.receiveMessages(batchSize, { maxWaitTimeInMs: 1000, maxTimeAfterFirstMessageInMs: 5000 })

Peek

Same as receiveMessages but does not mark the message as complete so it can still be received by other services once the peek lock expires.

const receiver = new MessageReceiver(config, action)
// receive a maximum of 10 messages
messages = await receiver.peekMessages(10)

// shutdown when needed
await receiver.closeConnection()

Handling a received message

Once a message is received through a peek lock, a response must be sent to Azure Service Bus before the lock expires otherwise Service Bus will resend the message.

If this is not the intended behaviour there are several responses that can be sent.

Complete

Message is complete and no further processing needed.

await receiver.completeMessage(message)

Dead Letter

Message cannot be processed by any client so should be added to dead letter queue.

await receiver.deadLetterMessage(message)

Abandon

Abandon processing of current message so it can be redelivered, potentially to another client.

await receiver.abandonMessage(message)

Defer

Defer message back to queue for later processing. It will not be redelivered to any client unless the receiver command supplies the sequence number as an option.

Further reading on deferring messages

// Defer
await receiver.deferMessage(message)

Licence

THIS INFORMATION IS LICENSED UNDER THE CONDITIONS OF THE OPEN GOVERNMENT LICENCE found at:

http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3

The following attribution statement MUST be cited in your products and applications when using this information.

Contains public sector information licensed under the Open Government license v3

About the licence

The Open Government Licence (OGL) was developed by the Controller of Her Majesty's Stationery Office (HMSO) to enable information providers in the public sector to license the use and re-use of their information under a common open licence.

It is designed to encourage use and re-use of information freely and flexibly, with only a few conditions.

defra.ffc-messaging's People

Contributors

faytoward avatar johnwatson484 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.