|
| 1 | +[](https://codecov.io/gh/ANcpLua/SWEN3.Paperless.RabbitMq) |
| 2 | +[](https://dotnet.microsoft.com/download/dotnet/10.0) |
| 3 | +[](https://www.nuget.org/packages/SWEN3.Paperless.RabbitMq/) |
| 4 | +[](https://github.com/ANcpLua/SWEN3.Paperless.RabbitMq/blob/main/LICENSE) |
| 5 | + |
| 6 | +# SWEN3.Paperless.RabbitMq |
| 7 | + |
| 8 | +RabbitMQ messaging library for .NET with SSE support. |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +```bash |
| 13 | +dotnet add package Paperless.RabbitMq |
| 14 | +``` |
| 15 | + |
| 16 | +## Configuration |
| 17 | + |
| 18 | +```json |
| 19 | +{ |
| 20 | + "RabbitMQ": { |
| 21 | + "Uri": "amqp://guest:guest@localhost:5672" |
| 22 | + } |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +### Basic Setup |
| 29 | + |
| 30 | +```csharp |
| 31 | +// Add RabbitMQ |
| 32 | +builder.Services.AddPaperlessRabbitMq(builder.Configuration); |
| 33 | + |
| 34 | +// With SSE support |
| 35 | +builder.Services.AddPaperlessRabbitMq(configuration, includeOcrResultStream: true); |
| 36 | +``` |
| 37 | + |
| 38 | +### Publishing |
| 39 | + |
| 40 | +```csharp |
| 41 | +var command = new OcrCommand(docId, fileName, storagePath); |
| 42 | +await publisher.PublishOcrCommandAsync(command); |
| 43 | + |
| 44 | +var result = new OcrEvent(jobId, "Completed", text, DateTimeOffset.UtcNow); |
| 45 | +await publisher.PublishOcrEventAsync(result); |
| 46 | +``` |
| 47 | + |
| 48 | +### Consuming |
| 49 | + |
| 50 | +```csharp |
| 51 | +await using var consumer = await factory.CreateConsumerAsync<OcrCommand>(); |
| 52 | + |
| 53 | +await foreach (var command in consumer.ConsumeAsync(cancellationToken)) |
| 54 | +{ |
| 55 | + try |
| 56 | + { |
| 57 | + // Process message |
| 58 | + await consumer.AckAsync(); |
| 59 | + } |
| 60 | + catch |
| 61 | + { |
| 62 | + await consumer.NackAsync(requeue: true); |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +### SSE Endpoint |
| 68 | + |
| 69 | +```csharp |
| 70 | +// Map endpoint |
| 71 | +app.MapOcrEventStream(); |
| 72 | + |
| 73 | +// Client-side |
| 74 | +const eventSource = new EventSource('/api/v1/ocr-results'); |
| 75 | +eventSource.addEventListener('ocr-completed', (event) => { |
| 76 | + const data = JSON.parse(event.data); |
| 77 | + console.log(data); |
| 78 | +}); |
| 79 | +``` |
| 80 | + |
| 81 | +## Message Types |
| 82 | + |
| 83 | +```csharp |
| 84 | +public record OcrCommand(Guid JobId, string FileName, string FilePath); |
| 85 | +public record OcrEvent(Guid JobId, string Status, string? Text, DateTimeOffset ProcessedAt); |
| 86 | +``` |
| 87 | + |
| 88 | +## License |
| 89 | + |
| 90 | +This project is licensed under the [MIT License](LICENSE). |
0 commit comments