Neurologically Paralyzing Mouseovers
    Have ideas to improve npm?Join in the discussion! »

    @cloudamqp/amqp-client

    1.1.2 • Public • Published

    amqp-client.js

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

    This library 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 send 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 make it easy for developers who already are familiar with AMQP to write browser apps that communicates directly with an AMQP server over WebSocket.

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

    Install

    npm install @cloudamqp/amqp-client --save

    For web browsers a rolled up version is available in dist/.

    Example usage

    Using AMQP in Node.js:

    import AMQPClient from '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.bodyString())
          await consumer.cancel()
        })
        await q.publish("Hello World", {deliveryMode: 2})
        await consumer.wait() // will block until consumer is cancled 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()

    Using AMQP over WebSockets in a browser:

    <!DOCTYPE html>
    <html>
      <head>
        <script type=module>
          import AMQPClient from './js/amqp-websocket-client.mjs'
    
          const textarea = document.getElementById("textarea")
          const input = document.getElementById("message")
    
          const tls = window.location.scheme === "https:"
          const url = `${tls ? "wss" : "ws"}://${window.location.host}`
          const amqp = new AMQPClient(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.bodyString() + "\n"
                msg.ack()
              })
            } catch (err) {
              console.error("Error", err, "reconnecting in 1s")
              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")
                setTimeout(start, 1000)
              }
              input.value = ""
            }
          }
    
          start()
        </script>
      </head>
      <body>
        <form>
          <textarea id="textarea" rows=10></textarea>
          <br/>
          <input id="message"/>
          <button type="submit">Send</button>
        </form>
      </body>
    </html>

    Keywords

    Install

    npm i @cloudamqp/amqp-client

    DownloadsWeekly Downloads

    19

    Version

    1.1.2

    License

    Apache-2.0

    Unpacked Size

    158 kB

    Total Files

    13

    Last publish

    Collaborators

    • avatar