|
| 1 | +# BabelQueue.Sqs |
| 2 | + |
| 3 | +Amazon SQS transport for [BabelQueue](https://babelqueue.com) — "Polyglot Queues, |
| 4 | +Simplified." Built on the AWS SDK for .NET and the framework-agnostic |
| 5 | +[`BabelQueue.Core`](https://www.nuget.org/packages/BabelQueue.Core). |
| 6 | + |
| 7 | +A canonical-envelope **publisher** and a URN-routed **consumer**, so an SQS-based .NET |
| 8 | +service speaks the same wire contract (envelope shape, URN identity, trace propagation) |
| 9 | +as the PHP/Laravel, Python, Go, Node and Java SDKs. Implements |
| 10 | +[§3 of the broker-bindings contract](https://babelqueue.com). |
| 11 | + |
| 12 | +## Install |
| 13 | + |
| 14 | +```bash |
| 15 | +dotnet add package BabelQueue.Sqs |
| 16 | +``` |
| 17 | + |
| 18 | +It pulls `BabelQueue.Core` and `AWSSDK.SQS` transitively. |
| 19 | + |
| 20 | +## Use |
| 21 | + |
| 22 | +```csharp |
| 23 | +using Amazon.SQS; |
| 24 | +using BabelQueue.Sqs; |
| 25 | + |
| 26 | +IAmazonSQS sqs = new AmazonSQSClient(); // your AWS config / credentials chain |
| 27 | +var url = "https://sqs.eu-central-1.amazonaws.com/123456789012/orders"; |
| 28 | + |
| 29 | +// produce |
| 30 | +var id = await new SqsPublisher(sqs, url) |
| 31 | + .PublishAsync("urn:babel:orders:created", new Dictionary<string, object?> { ["order_id"] = 1042 }); |
| 32 | + |
| 33 | +// consume |
| 34 | +var handlers = new Dictionary<string, BabelHandler> |
| 35 | +{ |
| 36 | + ["urn:babel:orders:created"] = async (env, message, ct) => |
| 37 | + { |
| 38 | + // env.Data, env.TraceId, env.Attempts ... |
| 39 | + }, |
| 40 | +}; |
| 41 | +var consumer = new SqsConsumer(sqs, url, handlers, new SqsConsumerOptions |
| 42 | +{ |
| 43 | + OnError = (err, env, msg) => Console.Error.WriteLine(err), |
| 44 | +}); |
| 45 | +await consumer.RunAsync(cancellationToken); // long-polls until cancelled |
| 46 | +``` |
| 47 | + |
| 48 | +FIFO: `new SqsPublisher(sqs, url, fifo: true)` (the URL must end in `.fifo`). For |
| 49 | +LocalStack/ElasticMQ, point the `AmazonSQSClient`'s `ServiceURL` there. |
| 50 | + |
| 51 | +## Contract mapping (§3) |
| 52 | + |
| 53 | +| Envelope | SQS | |
| 54 | +| :--- | :--- | |
| 55 | +| body | `MessageBody` (byte-identical across SDKs) | |
| 56 | +| `job` (URN) | `MessageAttributes.bq-job` | |
| 57 | +| `trace_id` | `MessageAttributes.bq-trace-id` | |
| 58 | +| `meta.id` | `MessageAttributes.bq-message-id` | |
| 59 | +| `meta.schema_version` | `MessageAttributes.bq-schema-version` (Number) | |
| 60 | +| `meta.lang` | `MessageAttributes.bq-source-lang` | |
| 61 | +| `meta.created_at` | `MessageAttributes.bq-created-at` (Number, ms) | |
| 62 | +| `attempts` | reconciled to `ApproximateReceiveCount − 1` on receive | |
| 63 | +| reserve / ack | visibility timeout → `DeleteMessage` | |
| 64 | + |
| 65 | +Retry is **SQS-native**: a throwing handler leaves the message undeleted, so SQS |
| 66 | +redelivers it after the visibility timeout (at-least-once). The poll loop never stops on |
| 67 | +a bad message — observe via `OnError` / `OnUnknownUrn`. The envelope is unchanged |
| 68 | +(`schema_version` stays `1`); SQS is purely additive. |
| 69 | + |
| 70 | +## Build & test |
| 71 | + |
| 72 | +```bash |
| 73 | +dotnet test |
| 74 | +``` |
| 75 | + |
| 76 | +`IAmazonSQS` is an interface, so the unit tests mock it with Moq — no AWS, no network. |
| 77 | + |
| 78 | +## License |
| 79 | + |
| 80 | +MIT |
0 commit comments