Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/paolosarti/nats.jl
NATS client in Julia
https://github.com/paolosarti/nats.jl
julia julialang messaging nats nats-client package publish-subscribe pubsub
Last synced: 24 days ago
JSON representation
NATS client in Julia
- Host: GitHub
- URL: https://github.com/paolosarti/nats.jl
- Owner: PaoloSarti
- License: mit
- Created: 2023-01-14T18:41:42.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-21T06:26:37.000Z (12 months ago)
- Last Synced: 2024-10-12T23:21:51.564Z (24 days ago)
- Topics: julia, julialang, messaging, nats, nats-client, package, publish-subscribe, pubsub
- Language: Julia
- Homepage:
- Size: 39.1 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
# NATS.jl
![Build Status](https://github.com/PaoloSarti/NATS.jl/actions/workflows/test.yaml/badge.svg)
[![codecov](https://codecov.io/gh/PaoloSarti/NATS.jl/graph/badge.svg?token=9GPEQS1B8O)](https://codecov.io/gh/PaoloSarti/NATS.jl)Unofficial client to NATS in Julia
## Supports
- publish-subscribe
- request-reply
- connect to multiple nats servers in a cluster
- channel-based non-blocking interaction## Usage
### Iterate subscription
```julia
using NATSnc = NATS.connect()
subject = "subject"
sub = subscribe(nc, subject)
sub_task = @async for msg in sub
println(msg)
endpublish(nc, subject, "hi!")
drain(nc)
```### Subscription callback
```julia
using NATSnc = NATS.connect()
subject = "subject"
sub = subscribe(nc, subject, println)
publish(nc, subject, "hi!")
publish(nc, subject, "hello!")
unsubscribe(nc, sub)publish(nc, subject, "wow!") # This will not arrive
drain(nc)
```### Request Reply
```julia
using NATSnc = NATS.connect()
subject = "hello"echo_sub = subscribe(nc, subject, msg -> publish(nc, msg.reply_to, msg.payload))
msg = request(nc, subject, "hey")
drain(nc)
```### Consume subscription in multiple threads
```julia
using NATSnc = NATS.connect()
subject = "hello"
sub = subscribe(nc, subject)
t = @async Threads.foreach(x -> println("ID: $(Threads.threadid()) - $x"), channel(sub))for i = 1:100
publish(nc, subject, "m: $i")
enddrain(nc)
```### Handle requests in multiple threads
```julia
using NATSnc = NATS.connect()
subject = "hello"
sub = subscribe(nc, subject)function handle_request(msg)
println("Thread: $(Threads.threadid()): arrived: $msg")
return publish(nc, msg.reply_to, msg.payload)
endt = @async Threads.foreach(handle_request, channel(sub))
# Launch requests in parallel to see that it uses multiple threads in the consumer
results = asyncmap(x -> request(nc, subject, "hey"), 1:100)drain(nc)
```## Missing features
- support for messages with headers
- handle reconnections
- handle info message from servers for cluster reconfiguration
- JetStream