Connect with AMQP
SharkMQ speaks AMQP 0-9-1, supported by client libraries in every common language. Connections use TLS only.
| Setting | Value |
|---|---|
| Host | mq.cdnshark.com |
| Port | 5671 (TLS) |
| Virtual host | Your broker id, for example c12-orders (shown in the portal) |
| User / password | A SharkMQ user and its secret |
| URL form | amqps://USER:SECRET@mq.cdnshark.com:5671/c12-orders |
Python (pika)
import pika, os
conn = pika.BlockingConnection(pika.URLParameters(os.environ["SHARKMQ_URL"]))
ch = conn.channel()
ch.queue_declare(queue="orders", durable=True) # replicated automatically
ch.confirm_delivery() # wait until it is safely stored
ch.basic_publish(exchange="", routing_key="orders", body=b"order 42",
properties=pika.BasicProperties(delivery_mode=2))
def handle(ch, method, props, body):
print(body); ch.basic_ack(method.delivery_tag)
ch.basic_qos(prefetch_count=10)
ch.basic_consume(queue="orders", on_message_callback=handle)
ch.start_consuming()
Node.js (amqplib)
const amqp = require("amqplib");
const conn = await amqp.connect(process.env.SHARKMQ_URL);
const ch = await conn.createConfirmChannel();
await ch.assertQueue("orders", { durable: true });
ch.sendToQueue("orders", Buffer.from("order 42"), { persistent: true });
await ch.waitForConfirms();
Good habits
- Durable queues, persistent messages, publisher confirms. A message is safe once the broker confirms it.
- Acknowledge after processing. A message that is not acknowledged goes back to the queue if your consumer disconnects.
- Reconnect. Connections can drop (a server restart, a network blip). Use your library's reconnect support, or retry.
- Prefetch. Set a prefetch (for example 10 to 100) so one slow consumer does not hold every message.
Every queue you declare is replicated on three servers, whatever arguments you pass. Exchanges, bindings and routing keys work as in any AMQP 0-9-1 broker.