Current section
Files
Jump to
Current section
Files
README.md
# gamqp
[](https://github.com/defgenx/gamqp/actions/workflows/ci.yml)
[](https://hex.pm/packages/gamqp)
[](https://hexdocs.pm/gamqp/)

An AMQP 0-9-1 client for Gleam, written in Gleam. It talks to the broker
over TCP or TLS itself, with no dependency on the Erlang `amqp_client`.
It is tested against RabbitMQ 4.
```sh
gleam add gamqp
```
## Usage
```gleam
import gamqp/amqp091/basic
import gamqp/amqp091/channel
import gamqp/amqp091/connection
import gamqp/amqp091/event
import gamqp/amqp091/message
import gamqp/amqp091/queue
import gleam/erlang/process
pub fn main() {
let assert Ok(config) = connection.from_uri("amqp://guest:guest@localhost")
let assert Ok(conn) = connection.open(config)
let assert Ok(ch) = channel.open(conn)
let assert Ok(_) = queue.declare(ch, "jobs", queue.declare_options())
let assert Ok(_) =
basic.publish(
ch,
exchange: "",
routing_key: "jobs",
message: message.text("resize photo 42") |> message.persistent,
options: basic.publish_options(),
)
// Deliveries arrive as messages on a subject you own.
let deliveries = process.new_subject()
let assert Ok(_) = basic.qos(ch, prefetch_count: 10, global: False)
let assert Ok(_) =
basic.consume(
ch,
queue: "jobs",
subject: deliveries,
options: basic.consume_options(),
)
let assert Ok(event.Delivered(delivery)) = process.receive(deliveries, 5000)
let assert Ok(_) = basic.ack(ch, delivery.delivery_tag, multiple: False)
let assert Ok(_) = connection.close(conn)
}
```
Modules follow the protocol's classes:
| Module | What it does |
|---|---|
| `gamqp/amqp091/connection` | Open and close connections, from a `Config` or an `amqp://` / `amqps://` URI |
| `gamqp/amqp091/channel` | Open and close channels |
| `gamqp/amqp091/exchange` | Declare, delete and bind exchanges (exchange-to-exchange bindings included) |
| `gamqp/amqp091/queue` | Declare, bind, purge and delete queues |
| `gamqp/amqp091/basic` | Publish, consume, get, ack, nack, reject, qos, returns |
| `gamqp/amqp091/confirm` | Publisher confirms |
| `gamqp/amqp091/tx` | Transactions |
| `gamqp/amqp091/message` | Message properties and builders |
| `gamqp/amqp091/table` | Field tables for arguments and headers |
### Errors
Every call returns `Result(_, AmqpError)`. Broker errors that close a
channel come back as `ChannelClosed(code, text)`. For example, a passive
declare of a missing queue gives `ChannelClosed(404, ...)`. The channel is
then unusable, but the connection and its other channels keep working.
### Publisher confirms
```gleam
let assert Ok(Nil) = confirm.select(ch)
// ... publish ...
case confirm.wait(ch, timeout: 5000) {
Ok(Nil) -> // the broker has every message
Error(core.Nacked) -> // at least one was refused
Error(other) -> // timeout, channel closed, ...
}
```
### TLS
`amqps://` URIs verify the broker against the system CA store. For
anything else, set `tls` in the config:
```gleam
connection.Config(
..config,
port: 5671,
tls: Some(
tls.verified()
|> tls.ca_certificates("/etc/ssl/broker-ca.pem")
|> tls.client_certificate(certificate: "client.pem", key: "client.key"),
),
)
```
## Development
```sh
docker run -d --rm --name gamqp-rabbitmq -p 5672:5672 rabbitmq:4
GAMQP_TEST_URI=amqp://guest:guest@localhost:5672 gleam test
```
Without `GAMQP_TEST_URI` only the unit tests run. How the client works
inside is described in [docs/architecture.md](docs/architecture.md).
## Releasing
Commits on `master` follow [Conventional Commits](https://www.conventionalcommits.org/).
On every push to `master`, [release-please](https://github.com/googleapis/release-please)
opens or updates a release pull request that bumps the version in `gleam.toml`
and `src/gamqp.gleam` and adds the new entries to `CHANGELOG.md`. A `feat` bumps
the minor version, a `fix` the patch, and a breaking change the major (0.x
included, so the first breaking change releases 1.0.0).
Merging the release pull request tags `vX.Y.Z` and creates the GitHub release.
Every push to `master` also publishes the version in `gleam.toml` to Hex if it
is not there yet, using the `HEXPM_API_KEY` secret of the `Hex` environment.
Hex holds releases published with a key until an owner approves them under
Dashboard > Staged releases on hex.pm (2FA required, within seven days), so
nothing is public before that.