This repository contains an example connecting node applications with a NATS Streaming Server by using node-nats-streaming npm package
- Skaffold for managing kubernetes and docker images
- NATS Docker Image as NATS Streaming Server
- npm package node-nats-streaming for interacting with NATS Streaming Server.
- Other libraries and types definitions (
ts-node-dev
,typescript
,@types/node
)
- subject: Is the channel that where we are pushing an event
- channel: Is what we are subscribing to
- subscription: Is what is actually listening to the channel and eventually receive some data
npm install
- If not isntalled, install skaffold from https://skaffold.dev/docs/install/
- In one terminal, initialize nats image by using
skaffold dev
For exposing our NATS Streaming Server, we will make a port forward to the running pod.
- Check your pod name with command
kubectl get pods
- Copy your running pod name (for example
nats-depl-7c95b5f667-zw5cc
) - Fordward the port
4222
with commandkubectl port-forward nats-depl-7c95b5f667-zw5cc 4222:4222
- After doing this, in a third terminal, we can run
npm run publish
and check if connection is OK (We should receivepublisher connected to NATS
log)
Publisher script (npm run publish
) will connect to nats server and create an example push of data to channel ticket:created
.
Listener script (npm run listen
) will connect to nats server and listen for data in channel ticket:created
.
To enable NATS monitoring, we need to forwar dpor 8222
.
- Check your pod name with command
kubectl get pods
- Write command
kubectl port-forward nats-depl-7c95b5f667-zw5cc 8222:8222
After doing this, you will be able to go to http://localhost:8222, here are some importante sites
- http://localhost:8222/streaming/channelsz: List of channels
- http://localhost:8222/streaming/channelsz?subs=1 Extended list of channels