nats-io / stan.js

Node.js client for NATS Streaming
Apache License 2.0
293 stars 49 forks source link

Invoking subscribe() on an existing channel, returns Error: can't find clientID #213

Closed bdushimi closed 4 months ago

bdushimi commented 1 year ago

I am trying to create a subscription to a channel ticket:created. A call to subscribe method just returns Error: can't find clientID.

Listener/Subscriber codes

import nats from 'node-nats-streaming';
import { randomBytes } from 'crypto';

const uniqueClientId = randomBytes(4).toString('hex');
const client = nats.connect('tickets', uniqueClientId, {
    url: 'http://localhost:4222'
})

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

    const opts = client.subscriptionOptions();
    opts.setStartWithLastReceived();
    const subscription = client.subscribe('ticket:created', 'orders-service-queue-group', opts);
})

Error logs:

Error: can't find clientID: ba692e24
    at Object.callback (/Users/dushi/dev-env/nodejs-projects/microservices-nodejs-react/node-level-up/nats-test/node_modules/node-nats-streaming/lib/stan.js:708:28)
    at Object.callback (/Users/dushi/dev-env/nodejs-projects/microservices-nodejs-react/node-level-up/nats-test/node_modules/nats/lib/nats.js:2081:16)
    at Client.processMsg (/Users/dushi/dev-env/nodejs-projects/microservices-nodejs-react/node-level-up/nats-test/node_modules/nats/lib/nats.js:1467:13)
    at Client.processInbound (/Users/dushi/dev-env/nodejs-projects/microservices-nodejs-react/node-level-up/nats-test/node_modules/nats/lib/nats.js:1340:14)
    at Socket.<anonymous> (/Users/dushi/dev-env/nodejs-projects/microservices-nodejs-react/node-level-up/nats-test/node_modules/nats/lib/nats.js:852:10)
    at Socket.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Socket.Readable.push (node:internal/streams/readable:234:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
[ERROR] 15:11:36 Error: can't find clientID: ba692e24

Any help would be much appreciated, thanks.

FYI, Publishing messages to ticket:created channel works just fine i.e no errors.

aricart commented 1 year ago

The connect you have is for nats, which possibly is getting reexported there - can you try:

const sc = require('node-nats-streaming').connect('tickets', uniqueClientId)
aricart commented 1 year ago

Also note, that stan.js is deprecated - if you are starting new work, please use JetStream - which is built into the current nats clients.

bdushimi commented 1 year ago

@aricart

I tried this 👇 but I still get the same error as described above.

The connect you have is for nats, which possibly is getting reexported there - can you try:

const sc = require('node-nats-streaming').connect('tickets', uniqueClientId)
aricart commented 1 year ago

I c/p your code:

nats-streaming-server -cid tickets --store MEMORY
[27324] 2023/02/27 13:22:51.913993 [INF] STREAM: Starting nats-streaming-server[tickets] version 0.25.3
[27324] 2023/02/27 13:22:51.914060 [INF] STREAM: ServerID: kVTeghEuBH7xbHOD4MH0sy
[27324] 2023/02/27 13:22:51.914062 [INF] STREAM: Go version: go1.19.5
[27324] 2023/02/27 13:22:51.914064 [INF] STREAM: Git commit: [5cd100e]
[27324] 2023/02/27 13:22:51.915403 [INF] Starting nats-server
[27324] 2023/02/27 13:22:51.915408 [INF]   Version:  2.9.11
[27324] 2023/02/27 13:22:51.915410 [INF]   Git:      [23ffc16]
[27324] 2023/02/27 13:22:51.915411 [INF]   Name:     NCNO6RLJC5HEWJXMTWNGI4IN7GB4TH7OWIHKNWSJYOF2KYRTA2KP63IW
[27324] 2023/02/27 13:22:51.915413 [INF]   ID:       NCNO6RLJC5HEWJXMTWNGI4IN7GB4TH7OWIHKNWSJYOF2KYRTA2KP63IW
[27324] 2023/02/27 13:22:51.915874 [INF] Listening for client connections on 0.0.0.0:4222
[27324] 2023/02/27 13:22:51.916030 [INF] Server is ready
[27324] 2023/02/27 13:22:51.944307 [INF] STREAM: Recovering the state...
[27324] 2023/02/27 13:22:51.944321 [INF] STREAM: No recovered state
[27324] 2023/02/27 13:22:51.944684 [INF] STREAM: Message store is MEMORY
[27324] 2023/02/27 13:22:51.944716 [INF] STREAM: ---------- Store Limits ----------
[27324] 2023/02/27 13:22:51.944719 [INF] STREAM: Channels:                  100 *
[27324] 2023/02/27 13:22:51.944722 [INF] STREAM: --------- Channels Limits --------
[27324] 2023/02/27 13:22:51.944724 [INF] STREAM:   Subscriptions:          1000 *
[27324] 2023/02/27 13:22:51.944726 [INF] STREAM:   Messages     :       1000000 *
[27324] 2023/02/27 13:22:51.944728 [INF] STREAM:   Bytes        :     976.56 MB *
[27324] 2023/02/27 13:22:51.944730 [INF] STREAM:   Age          :     unlimited *
[27324] 2023/02/27 13:22:51.944732 [INF] STREAM:   Inactivity   :     unlimited *
[27324] 2023/02/27 13:22:51.944734 [INF] STREAM: ----------------------------------
[27324] 2023/02/27 13:22:51.944736 [INF] STREAM: Streaming Server is ready
[27324] 2023/02/27 13:23:48.355679 [INF] STREAM: Channel "ticket:created" has been created

Note the last line, and the client:

node main.js
Listener connected to NATS
bdushimi commented 1 year ago

@aricart for some reasons, I do not still get it to work! I'd like to see your main.js file and package.json file. I could start from there.

aricart commented 1 year ago

The message is an error returned in response to the subscription by the server. From what I can tell that client ID is not found by the stan server - which is weird because the client is actually connected - is there as weird cluster or something going on?

Perhaps @kozlovic can tell from looking at the message?

aricart commented 1 year ago
import nats from 'node-nats-streaming';
import { randomBytes } from 'crypto';

const uniqueClientId = randomBytes(4).toString('hex');
const client = nats.connect('tickets', uniqueClientId, {
    url: 'nats://localhost:4222'
})

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

    const opts = client.subscriptionOptions();
    opts.setStartWithLastReceived();
    const subscription = client.subscribe('ticket:created', 'orders-service-queue-group', opts);
})
{
  "name": "s_test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "node-nats-streaming": "^0.3.2"
  }
}
bdushimi commented 1 year ago

The message is an error returned in response to the subscription by the server. From what I can tell that client ID is not found by the stan server - which is weird because the client is actually connected - is there as weird cluster or something going on?

Perhaps @kozlovic can tell from looking at the message?

@aricart No cluster actually!

I just have 1 publisher, sending messages to the channel ticket:created and it seems working fine i.e no errors. Here're the publisher codes

import nats from 'node-nats-streaming'

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

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

    const data = JSON.stringify({
        id: '123',
        title: 'concert',
        price: 20
    })

    client.publish('ticket:created', data, ()=> {
        console.log('Event published')
    })
})
aricart commented 1 year ago

cluster is the "stan cluster" which has an unique name - the 'tickets' argument in your client.

bdushimi commented 1 year ago

@kozlovic Maybe you can help. I am still having the same error as described above.