nats.erl
A simple NATS client library for Erlang/OTP.
This project is still WIP. Any of the implementations, including exported APIs, may change drastically and frequently.
Features
- Connect to a NATS server
- A subscriber receiving messages (publish/subscribe)
- A subscriber receiving messages (request/reply)
- A publisher sending messages
- Keep-alive (PING/PONG) handling
- Unsubscribing
- Using TLS
- Authentications
- Token Authentication
- Username/Password credentials
- TLS Certificate
- NKEY with Challenge
- Decentralized JWT Authentication/Authorization
Usage
See Subscriber/Publisher examples for details.
%% Start gen_server with network info.
nats:start_link(#{remote_address => Host,
remote_port => P,
ping_interval => 1000}),
%% Connect to your NATS server.
{ok, Info} = nats:connect(),
%% Subscribe or publish with required parameters.
ok = nats:subscribe(Subject, SID),
ok = nats:publish(Subject, Message),
%% After subscribing, received messages are sent to your Pid.
receive
Msg ->
io:format("Received: ~p~n", [Msg]),
end
Running examples
Compile and run the sample subscriber.
rebar3 compile
./example/subscribe.escript 127.0.0.2 4222 foo.bar 11
Then run the sample publisher on another terminal session.
./example/publish.escript 127.0.0.2 4222 foo.bar "msg to publish"
Author(s)
Yoshiyuki Kurauchi (Website / Twitter)
We're always open to welcome co-authors! Please feel free to talk to us.