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
- Connecting 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.
natserl:start_link(#{remote_address => Host, % server address
remote_port => P, % server port
ping_interval => 1000 % interval to send PING
}),
%% Connect to your NATS server.
%% Info has the contents of INFO message from the server.
{ok, Info} = natserl:connect(),
%% Subscribe or publish with required parameters.
ok = natserl:subscribe(Subject, SID),
ok = natserl:publish(Subject, Message),
%% After subscribing, received messages are sent to your Pid.
receive
Msg ->
io:format("Received: ~p~n", [Msg]),
end
Running examples
Compile everything first in the project root.
rebar3 compile
Run the subscriber example.
Params: <server address> <server port> <subject> <sid>.
./example/subscribe.escript 127.0.0.2 4222 foo.bar 11
Then run the publisher example on another terminal session.
Params: <server address> <server port> <subject> <message>.
./example/publish.escript 127.0.0.2 4222 foo.bar "msg to publish"
Author(s)
Yoshiyuki Kurauchi (Website / Twitter), with a lot of help from my teammates in Working Group Two.
We're always open to welcome co-authors! Please feel free to talk to us.