skip to Main Content

my publisher.ts file:

import nats from 'node-nats-streaming';

const stan = nats.connect('ticketing', 'abc', {
  url: 'http://localhost:4222',
});

stan.on('connect', () => {
  console.log('Publisher connected to NATS');

  });
});

my port forwarding command:

kubectl port-forward {nats-deployment pod name} 4222:4222

my publish command in my nats-test folder:

"publish": "ts-node-dev --rs --notify false src/publisher.ts"

my nats-depl.yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nats-depl
spec:
  selector:
    matchLabels:
      app: nats
  template:
    metadata:
      labels:
        app: nats
    spec:
      containers:
      - name: nats
        image: nats-streaming:0.17.0
        args: [
          '-p',
          '4222',
          '-m',
          '8222',
          '-hbi',
          '5s',
          '-hbt',
          '5s',
          '-hbf',
          '2',
          '-SD',
          '-cid',
          '-ticketing'
        ]
---
apiVersion: v1
kind: Service
metadata:
  name: nats-srv
spec:
  selector:
    app: nats
  ports:
  - name: client
    protocol: TCP
    port: 4222
    targetPort: 4222
  - name: monitoring
    protocol: TCP
    port: 8222
    targetPort: 8222  

not able to connect to nats client.

2

Answers


  1. The first argument of the connect function is the cluster ID. You can check your NATS cluster’s ID in the logs of the container where it is running. The cluster ID is underlined with green in the picture.

    enter image description here

    Thus, the connect function should be:

    const stan = nats.connect('test-cluster', 'abc', {
      url: 'http://localhost:4222',
    });
    

    Instead of:

    const stan = nats.connect('testing', 'abc', {
      url: 'http://localhost:4222',
    });
    
    Login or Signup to reply.
  2. In your nats-depl.yaml file,
    The last entry in your args array is -ticketing, which is your clusterId

    remove from ticketing entry

    const stan = nats.connect('ticketing', 'abc', {
      url: 'http://nats-srv:4222', // nats-serv is your nats service name
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search