Introduction
Configuration
Language Support
AMQP 0-9-1 Overview
More Exchange Types
More Consumer Features
Queue Deep-dive
Other Features
Reliable Message Delivery
High Availability
Monitoring
Management HTTP API
Tutorials
Networking
LavinMQ CLI
AMQP Crystal Client
These examples cover the basics of an AMQP 0-9-1 client for Crystal. You can install your broker locally, or create a hosted broker with CloudAMQP. Just replace the connection URL with the one for your broker.
Sample code can be found at github.
Installation
Add the dependency to your shard.yml
:
Run shards install
Hello World!
In this example we will write two small programs in Crystal; a producer (sender) that sends a single message, and a consumer (receiver) that receives messages and prints them out.
Publisher
Our first program HelloWorld_publisher.cr
will send a single message “Hello World!” to a queue named “hello”.
Consumer
Our second program HelloWorld_consumer.cr
will receive messages from the queue named “hello” and print them on the screen.
Work Queues
In this example, we will create a work queue to distribute time-consuming tasks among multiple consumers. Each work will be delivered to exactly one consumer. If we are building up a backlog of work, we can easily scale by adding more consumers.
Publisher
Consumer
Publish/Subscribe
In this example, we will publish a message to be delivered to multiple consumers by using a fanout exchange.
Publisher
Consumer
Routing
In this example, we will publish a message to a direct exchange to be delivered to selective consumers by using routing keys. Add what routing key you want for your consumer and then publish messages with any of those routing keys. Publish a message with crystal send.cr routing_key message, for example, crystal send.cr error “This is an error message” and it will be routed to the consumer with that binding.
Publisher
Consumer
Topics
In this example, we will publish a message to a topic exchange to be delivered to selective consumers based on multiple criteria.
A *
(star) can substitute for exactly one word.
A #
(hash) can substitute for zero or more words.
Add what routing key you want for your consumer with stars and hashes, and then publish messages with any of those routing keys. Publish a message with crystal send.cr routing_key message, for example, crystal send.cr lazy-rabbit “This is a lazy rabbit” and it will be routed to the consumer with that binding.
Sending
Reciving
Ready to take the next steps? Here are some things you should keep in mind:
Managed LavinMQ instance on CloudAMQP
LavinMQ has been built with performance and ease of use in mind - we've benchmarked a throughput of about 1,000,000 messages/sec. You can try LavinMQ without any installation hassle by creating a free instance on CloudAMQP. Signing up is a breeze.
Help and feedback
We welcome your feedback and are eager to address any questions you may have about this piece or using LavinMQ. Join our Slack channel to connect with us directly. You can also find LavinMQ on GitHub.