• Stars
    star
    186
  • Rank 201,174 (Top 5 %)
  • Language
    TypeScript
  • License
    Apache License 2.0
  • Created over 3 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

AMQP 0-9-1 TypeScript client both for Node.js and browsers (using WebSocket)

amqp-client.js

AMQP 0-9-1 TypeScript client both for Node.js and browsers (using WebSocket). This library is intended to replace all other Node.js AMQP libraries.

API documentation.

This library is Promise-based and hence works very well with async/await. It's secure by default, for instance, publishes aren't fulfilled until either the data has been sent on the wire (so that back propagation is respected), or if the channel has Publish Confirms enabled, it isn't fulfilled until the server has acknowledged that the message has been enqueued.

The library was developed so to make it easy for developers who already are familiar with AMQP to write browser apps that communicates directly with an AMQP server over WebSocket.

Support

The library is developed and supported by CloudAMQP, the largest hosted RabbitMQ provider in the world.

Install

npm install @cloudamqp/amqp-client --save

Start node with --enable-source-maps to get proper stacktraces as the library is transpiled from TypeScript.

Example usage

Using AMQP in Node.js:

import { AMQPClient } from '@cloudamqp/amqp-client'

async function run() {
  try {
    const amqp = new AMQPClient("amqp://localhost")
    const conn = await amqp.connect()
    const ch = await conn.channel()
    const q = await ch.queue()
    const consumer = await q.subscribe({noAck: true}, async (msg) => {
      console.log(msg.bodyToString())
      await consumer.cancel()
    })
    await q.publish("Hello World", {deliveryMode: 2})
    await consumer.wait() // will block until consumer is canceled or throw an error if server closed channel/connection
    await conn.close()
  } catch (e) {
    console.error("ERROR", e)
    e.connection.close()
    setTimeout(run, 1000) // will try to reconnect in 1s
  }
}

run()

WebSockets

This library can be used in the browser to access an AMQP server over WebSockets. For servers such as RabbitMQ that doesn't (yet?) support WebSockets natively a WebSocket TCP relay have to be used as a proxy. More information can be found in this blog post.

For web browsers a compiled and rolled up version is available at https://github.com/cloudamqp/amqp-client.js/releases.

Using AMQP over WebSockets in a browser:

<!DOCTYPE html>
<html>
  <head>
    <script type=module>
      import { AMQPWebSocketClient } from './js/amqp-websocket-client.mjs'

      const textarea = document.getElementById("textarea")
      const input = document.getElementById("message")

      const tls = window.location.protocol === "https:"
      const url = `${tls ? "wss" : "ws"}://${window.location.host}`
      const amqp = new AMQPWebSocketClient(url, "/", "guest", "guest")

      async function start() {
        try {
          const conn = await amqp.connect()
          const ch = await conn.channel()
          attachPublish(ch)
          const q = await ch.queue("")
          await q.bind("amq.fanout")
          const consumer = await q.subscribe({noAck: false}, (msg) => {
            console.log(msg)
            textarea.value += msg.bodyToString() + "\n"
            msg.ack()
          })
        } catch (err) {
          console.error("Error", err, "reconnecting in 1s")
          disablePublish()
          setTimeout(start, 1000)
        }
      }

      function attachPublish(ch) {
        document.forms[0].onsubmit = async (e) => {
          e.preventDefault()
          try {
            await ch.basicPublish("amq.fanout", "", input.value, { contentType: "text/plain" })
          } catch (err) {
            console.error("Error", err, "reconnecting in 1s")
            disablePublish()
            setTimeout(start, 1000)
          }
          input.value = ""
        }
      }

      function disablePublish() {
        document.forms[0].onsubmit = (e) => { alert("Disconnected, waiting to be reconnected") }
      }

      start()
    </script>
  </head>
  <body>
    <form>
      <textarea id="textarea" rows=10></textarea>
      <br/>
      <input id="message"/>
      <button type="submit">Send</button>
    </form>
  </body>
</html>

Performance

Messages with a 1-byte body, no properties:

Client Publish rate Consume rate
amqp-client.js 300.000 msgs/s 512.000 msgs/s
amqplib 172.000 msgs/s 519.000 msgs/s

Messages with a 1-byte body, and all properties, except headers:

Client Publish rate Consume rate
amqp-client.js 144.000 msgs/s 202.000 msgs/s
amqplib 110.000 msgs/s 251.000 msgs/s

Messages with a 1-byte body, and all properties, including headers:

Client Publish rate Consume rate
amqp-client.js 70.000 msgs/s 89.000 msgs/s
amqplib 60.000 msgs/s 99.000 msgs/s

The reason amqp-client is somewhat slower to consume is that to maintain browser compatibility for the websocket client, DataView are used for parsing the binary protocol instead of Buffer.

Module comparison

Client Runtime dependencies Lines of code
amqp-client.js 0 1743
amqplib 14 6720 (w/o dependencies)

More Repositories

1

lavinmq

Lightweight and fast AMQP (0-9-1) server
Crystal
421
star
2

amqproxy

An intelligent AMQP proxy, with connection and channel pooling/reusing
Crystal
324
star
3

amqp-client.cr

An AMQP 0-9-1 client for Crystal
Crystal
66
star
4

android-example

Java
54
star
5

terraform-provider-cloudamqp

Terraform Provider for CloudAMQP
Go
35
star
6

websocket-tcp-relay

Expose any TCP server as a WebSocket endpoint
Crystal
29
star
7

rabbitmq-vshovel

RabbitMQ vShovel plugin
Erlang
25
star
8

amqp-client.rb

Modern AMQP 0-9-1 Ruby client
Ruby
19
star
9

amqpcat

CLI tool for publishing to and consuming from AMQP servers
Crystal
18
star
10

amqp-sse

Ruby Sinatra app illustrating how to use Server Sent Events and AMQP for realtime updates
Ruby
17
star
11

python-amqp-example

How to connect from Python to CloudAMQP
Python
14
star
12

java-amqp-example

Java
11
star
13

rabbitmq-delayed-messages

Ruby snippet, demonstrates how to implement delayed messages in RabbitMQ
10
star
14

nodejs-amqp-example

Example project showing how to connect to CloudAMQP from Node.js
JavaScript
10
star
15

amq-protocol.cr

An AMQP 0.9.1 serialization library for Crystal
Crystal
10
star
16

msg_store_eleveldb_index

eLevelDB backend for RabbitMQ's message index store
Erlang
9
star
17

web-stomp-example

Exampel app in sinatra/javascript to use RabbitMQ Web-stomp
HTML
7
star
18

rmqrecover

Recovers messages from a RabbitMQ data directory
Crystal
6
star
19

php-amqplib-example

How to connect to CloudAMQP from a PHP app
PHP
5
star
20

dotnetcore-amqp-example

C#
4
star
21

DotNetAmqpExample

Simple usage of AMQP demonstrated in a ASP.NET MVC app
C#
4
star
22

clojure-amqp-example

A Clojure example project showing how to connect, publish and consume a message via CloudAMQP
Clojure
2
star
23

php-amqplib-ssl-example

2
star
24

erlang-packages

Erlang debian packages
Dockerfile
1
star
25

amqpshovel

High performance AMQP shovel
Crystal
1
star
26

cloudamqp-connector

Deprecated, please use https://github.com/mulesoft/mule-transport-amqp instead
Java
1
star
27

lavinmq-action

GitHub Action to run LavinMQ
1
star
28

get-me-hired-producer

Making the job search process easier
Python
1
star
29

mesque

A Resque compatible work library using RabbitMQ as backend for relability and performance
Ruby
1
star
30

lavinmq-tutorials

Beginner friendly LavinMQ tutorials that you can't refuse :)
JavaScript
1
star
31

rabbitmq-summit

Website for the rabbitmq-summit
SCSS
1
star
32

rabbitmq-integration-demos

Integrating RabbitMQ with other technologies
Python
1
star
33

websockets-rabbitmq-benchmarks

Benchmark web-mqtt, web-stomp, and web-amqp
JavaScript
1
star