Skip to content

This project presents a practical example of the concepts of Nats. The App Client exposes some endpoints in NodeJs + Express. During application startup, subscribers are added to queues to listen to subjects. When a client invokes the endpoints, an event is published, and Nats persist that event in a stream. Subscribers are notified about the ev…

araujoronald/nats-ts-node

Repository files navigation

Nats-ts-node

This project presents a practical example of the concepts of Nats. The App Client exposes some endpoints in NodeJs + Express. During application startup, subscribers are added to queues to listen to subjects. When a client invokes the endpoints, an event is published, and Nats persist that event in a stream. Subscribers are notified about the event, process them, and acknowledge Nats about the consumed event.

Below, show the resumed architecure

Start the environment

First, you need create a .env file on the root folder, and set the follow keys:

  • NATS_CLIENT_ID_PREFIX=client-foot-
  • NATS_STREAM_NAME=football
  • NATS_SERVER_URL=nats://nats:4222

Run the docker compose

docker-compose up

This command will start the Nats Server and three NodeJS Applications. The app node1 is configurated to be only publisher, other node apps (2 and 3) are publisher and subscriber.

The endpoints

/match-started

HTTP return 200 OK, the subscriber will be consume the event and acknowledge.

/goal

HTTP return 200 OK, the subscriber will be consume the event and acknowledge.

/goal-offside

HTTP return 200 OK, the subscriber will be consume the event, throw an error and notify Nats with non acknowledge. The message won´t be resent.

/goal-checking-var

HTTP return 200 OK, the subscriber will be consume the event, and the timeout proccessing. The message will be resent. We know, VAR delay the match excessively.

Nats CLI

Download Nats CLI https://github.com/nats-io/natscli/releases

Connect to Nats Server

nats context add nats --server 0.0.0.0:4222 --description "NATS Docker local" --select

Subscribe on subject

nats sub futebol.brasileiro

Publish on topic

nats pub futebol.brasileiro "Bora Bahea"

Nats Jetstream

Create a stream

nats stream add futebol_stream

Info about stream

nats stream info futebol_stream

Publish on a stream

nats pub brasileirao --count=1000 --sleep 1s "publication #{{Count}} @ {{TimeStamp}}"

Add a consumer

nats consumer add

Consume a message

nats consumer next futebol_stream [consumer-name] --count 1000

Stop container (don´t receive messages)

docker container stop

Start container (receive messages)

docker container start <CONTAINER-ID>

Remove a message

nats stream rmm

Stream report

nats stream report

Remove all messages

nats stream purge

List all messages on stream

nats stream view [--subject [subject]]

Remove a messages from stream

nats stream rmm [stream] [id_message]

About

This project presents a practical example of the concepts of Nats. The App Client exposes some endpoints in NodeJs + Express. During application startup, subscribers are added to queues to listen to subjects. When a client invokes the endpoints, an event is published, and Nats persist that event in a stream. Subscribers are notified about the ev…

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published