Socket
Book a DemoInstallSign in
Socket

amqp-simple-pub-sub

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

amqp-simple-pub-sub

A Pub Sub system that uses AMQP Messaging to exchange data between services

latest
Source
npmnpm
Version
1.2.2
Version published
Maintainers
1
Created
Source

AMQP Simple Pub Sub

A Pub Sub system that uses AMQP messaging to exchange data between services.

NPM

To Use

You project needs to be using at least Node version 10, and ideally Node 18 (LTS) or later.

npm install amqp-simple-pub-sub

Create a Publisher

const { makePublisher } = require('amqp-simple-pub-sub')
const publisher = makePublisher({ exchange: 'testService' })

Publish a message

await publisher.start()
publisher.publish('test', 'Hello World')

Create a Subscriber

const { makeSubscriber } = require('amqp-simple-pub-sub')

const subscriber = makeSubscriber({
  exchange: 'testService',
  queueName: 'testQueue',
  routingKeys: ['test']
})

Subscribe to a queue and listen for messages

const handler = message => {
  console.log('Message Received', message)
  subscriber.ack(message)
}

subscriber.start(handler)

Publisher

The full options object is as follows

{
  type: 'topic', // the default
  url: 'amqp://localhost', // the default
  exchange: 'you must provide this', // it's the name of your service usually
  onError: err => { // optional
    console.error('A connection error happened', err) // or do something clever
  },
  onClose: () => { // optional
    console.log('The connection has closed.') // or do something clever
  },
  ...otherOptions // anything else you pass in gets passed directly to `amqp.connect`
}

Subscriber

The full options object is as follows

{
  type: 'topic', // the default
  url: 'amqp://localhost', // the default
  exchange: 'you must provide this', // it's the name of your service usually
  queueName: 'you must also provide this', // give your queue a name
  routingKeys: ['an', 'array', 'of', 'routingKeys'], // optional.  Uses [queueName] otherwise.
  onError: err => { // optional
    console.error('A connection error happened', err) // or do something clever
  },
  onClose: () => { // optional
    console.log('The connection has closed.') // or do something clever
  },
  ...otherOptions // anything else you pass in gets passed directly to `amqp.connect`
}

Other Options

As outlined above both createPublisher and createPublisher also accept other options.

These are passed straight onto amqp.connect under the hood. The options are:

  • clientProperties: see connect.js
  • credentials
  • keepAlive
  • keepAliveDelay
  • noDelay
  • servername
  • timeout

Examples

See some examples in the tests, and also:

  • amqp-delegate — A library that simplifies, to the point of triviality, use of AMQP based remote workers.
  • ampq-event-tester — A Dockerised and configurable utility to help integration-test your AMQP services.

Development

Branches

BranchTestsCode CoverageAuditComments
developCircleCIcodecovVulnerabilitiesWork in progress
mainCircleCIcodecovVulnerabilitiesLatest release

Prerequisites

Initialisation

npm install

To Start the queue server for integration testing

docker compose up -d

Runs Rabbit MQ.

Test it

  • npm test — runs the unit tests (quick and does not need rabbitmq running)
  • npm run test:unit:cov — runs the unit tests with code coverage (does not need rabbitmq)
  • npm run test:integration — runs the integration tests (needs rabbitmq)

Lint it

npm run lint

Contributing

Please see the contributing notes.

Keywords

amqp

FAQs

Package last updated on 08 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.