https://github.com/brianpursley/npgmq
A .NET client for Postgres Message Queue (PGMQ).
https://github.com/brianpursley/npgmq
csharp dotnet pgmq postgres postgresql queues
Last synced: 11 months ago
JSON representation
A .NET client for Postgres Message Queue (PGMQ).
- Host: GitHub
- URL: https://github.com/brianpursley/npgmq
- Owner: brianpursley
- License: mit
- Created: 2023-11-18T18:14:44.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-08T13:43:50.000Z (almost 2 years ago)
- Last Synced: 2024-11-17T12:17:00.834Z (over 1 year ago)
- Topics: csharp, dotnet, pgmq, postgres, postgresql, queues
- Language: C#
- Homepage:
- Size: 58.6 KB
- Stars: 16
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Npgmq
A .NET client for [Postgres Message Queue](https://github.com/tembo-io/pgmq) (PGMQ).
[](https://github.com/brianpursley/Npgmq/actions/workflows/build.yml)
[](https://www.nuget.org/packages/Npgmq/)

## Compatibility
* pgmq >= 0.31.0
## Installation
To install the package via [Nuget](https://www.nuget.org/packages/Npgmq/), run the following command:
```shell
dotnet add package Npgmq
```
## Usage
Here is an example that uses Npgmq to create a queue and then send/read/archive a message:
```csharp
using Npgmq;
var npgmq = new NpgmqClient("");
await npgmq.InitAsync();
await npgmq.CreateQueueAsync("my_queue");
var msgId = await npgmq.SendAsync("my_queue", new MyMessageType
{
Foo = "Test",
Bar = 123
});
Console.WriteLine($"Sent message with id {msgId}");
var msg = await npgmq.ReadAsync("my_queue");
if (msg != null)
{
Console.WriteLine($"Read message with id {msg.MsgId}: Foo = {msg.Message?.Foo}, Bar = {msg.Message?.Bar}");
await npgmq.ArchiveAsync("my_queue", msg.MsgId);
}
```
This example assumes you have defined a class called `MyMessageType` with the structure something like:
```csharp
public class MyMessageType
{
public string Foo { get; set; } = null!;
public int Bar { get; set; }
}
```
You can send and read messages as JSON strings, like this:
```csharp
var msgId = await npgmq.SendAsync("my_queue", "{\"foo\":\"Test\",\"bar\":123}");
Console.WriteLine($"Sent message with id {msgId}");
var msg = await npgmq.ReadAsync("my_queue");
if (msg != null)
{
Console.WriteLine($"Read message with id {msg.MsgId}: {msg.Message}");
await npgmq.ArchiveAsync("my_queue", msg.MsgId);
}
```
You can pass your own `NpgsqlConnection` to the `NpgmqClient` constructor, like this:
```csharp
using var myConnection = new NpgsqlConnection("");
var npgmq = new NpgmqClient(myConnection);
```
## Database Connection
Npgmq uses [Npgsql](https://www.npgsql.org/) internally to connect to the database.
### Using a Connection String
If you pass an [Npgsql connection string](https://www.npgsql.org/doc/connection-string-parameters.html) to the `NpgmqClient` constructor, it will use this connection string to create an [`NpgsqlConnection`](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html) object internally, and the connection lifetime will be managed by NpgmqClient.
### Using a Connection Object
If you pass an [`NpgsqlConnection`](https://www.npgsql.org/doc/api/Npgsql.NpgsqlConnection.html) object to the `NpgmqClient` constructor, it will use this connection instead of creating its own.