https://github.com/kriuchkov/nats-stream
nats-stream is a package for working with the NATS messaging system, providing type-safe interaction with NATS using Go generics.
https://github.com/kriuchkov/nats-stream
golang nats nats-streaming
Last synced: 2 months ago
JSON representation
nats-stream is a package for working with the NATS messaging system, providing type-safe interaction with NATS using Go generics.
- Host: GitHub
- URL: https://github.com/kriuchkov/nats-stream
- Owner: kriuchkov
- License: mit
- Created: 2025-07-24T17:26:53.000Z (3 months ago)
- Default Branch: master
- Last Pushed: 2025-07-24T17:50:32.000Z (3 months ago)
- Last Synced: 2025-07-25T14:16:39.069Z (3 months ago)
- Topics: golang, nats, nats-streaming
- Language: Go
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nats-stream
## Overview
`nats-stream` is a package for working with the NATS messaging system, providing type-safe interaction with NATS using Go generics.
## Key Features
- **Type-safe Interface**: Uses Go generics to work with messages of specific types
- **Simple Configuration**: Easily configured through the Options structure
- **JSON by Default**: Automatically handles JSON serialization/deserialization
- **Custom Unmarshalers**: Supports custom deserialization logic
- **Validation**: Uses validator for configuration correctness validation
- **Logging**: Includes zerolog integration for convenient logging## Usage Example
```go
package mainimport (
"context"
nats "github.com/kriuchkov/nats-stream"
)type Message struct {
Content string `json:"content"`
}func main() {
// Connection setup
opts := &nats.Options[Message]{
URL: "nats://localhost:4222",
}
// Client creation
client, err := nats.New(opts)
if err != nil {
panic(err)
}
// Message publishing
msg := &Message{Content: "Hello NATS"}
err = client.Publish(context.Background(), "my.subject", msg)
if err != nil {
panic(err)
}
// Message subscription
err = client.Subscribe(context.Background(), "my.subject", func(subject string, msg *Message) error {
// Handle received message
return nil
})
if err != nil {
panic(err)
}
}
```This package provides a simple and type-safe way to work with NATS, eliminating the need to manually handle message serialization and deserialization, and helps avoid runtime errors related to type mismatches.