Skip to content

lenkan/deno-amqp

Repository files navigation

deno-amqp

CI deno doc

AMQP 0.9.1 implementation for https://deno.land/. The library is implemented to connect to a RabbitMQ broker. For testing purposes, an instance can be started using docker:

docker run -d --rm -p 15672:15672 -p 5672:5672 rabbitmq:3-management

Usage

Consume messages

import { connect } from "https://deno.land/x/amqp/mod.ts";

const connection = await connect();
const channel = await connection.openChannel();

const queueName = "my.queue";
await channel.declareQueue({ queue: queueName });
await channel.consume(
  { queue: queueName },
  async (args, props, data) => {
    console.log(JSON.stringify(args));
    console.log(JSON.stringify(props));
    console.log(new TextDecoder().decode(data));
    await channel.ack({ deliveryTag: args.deliveryTag });
  },
);

Publish messages

import { connect } from "https://deno.land/x/amqp/mod.ts";

const connection = await connect();
const channel = await connection.openChannel();

const queueName = "my.queue";
await channel.declareQueue({ queue: queueName });
await channel.publish(
  { routingKey: queueName },
  { contentType: "application/json" },
  new TextEncoder().encode(JSON.stringify({ foo: "bar" })),
);

await connection.close();

More examples

See examples/. Start the example consumer by running

deno run --allow-net examples/consume_message.ts queue

Then, send a message to the same queue using the default exchange using the example publisher

deno run --allow-net examples/publish_message.ts queue

Documentation

See autogenerated deno docs at: https://doc.deno.land/https/deno.land/x/amqp/mod.ts

Development

Testing

Run unit tests

deno test src/

Run module tests (requires an AMQP broker running on localhost)

deno test --allow-net module_test/