Simple NATS client and API
this solution contain two main projects
- NATS server with JStream enabled running locally at localhost:4222
- creating a stream inside the NATS server
- .Net5
The easiest way to get a nats server running locally with JStream enabled is using docker, run this command
docker run -d -p 4222:4222 nats -js
docker run -ti --network host natsio/nats-box
then inside the container run:
nats str add SNOWCHAT --subjects "SNOWCHAT" --ack --max-msgs=-1 --max-bytes=-1 --max-age=1y --storage file --retention limits --max-msg-size=-1 --discard old --replicas 1 --dupe-window="2m"
to run the project create folder somewhere in your computer clone the git repo into it then run these commands
cd SnowChat
dotnet run --project SnowChat.Konsol CLIENTNAME
- replace the CLIENTNAME with you desired client name, leave it empty and it will be populated with a random GUID.
open another terminal in the same directory and repeat the last two steps to get another client running
start writing text on either terminal and it will be send to all other online clients! DA be mindful, empty values will be discarded, quit will quit the client.
in the same directory open a terminal and type
dotnet run --project SnowChat.API
- open your web browser and surf to url printed on the terminal output,
probably it's http://localhost:5000/swagger
the API is pretty straight forward, one get endpoint to get all messages
and one post to send a new message.
these stuff skipped for brevity
- security
- tests
- best practices