moleculerjs / moleculer

:rocket: Progressive microservices framework for Node.js
https://moleculer.services/
MIT License
6.16k stars 586 forks source link

Can't connect to NATS transporter: "client.on is not a function" #893

Closed alekbarszczewski closed 3 years ago

alekbarszczewski commented 3 years ago
import { ServiceBroker } from 'moleculer';

const broker = new ServiceBroker({
  transporter: 'NATS',
});

broker.createService({
  name: 'service_a',
  actions: {
    testA() {
      return  'A';
    }
  }
});

broker.start();

output:

[2021-03-18T11:49:41.637Z] INFO  mbp-10239/BROKER: Moleculer v0.14.12 is starting...
[2021-03-18T11:49:41.639Z] INFO  mbp-10239/BROKER: Namespace: <not defined>
[2021-03-18T11:49:41.639Z] INFO  mbp-10239/BROKER: Node ID: mbp-10239
[2021-03-18T11:49:41.640Z] INFO  mbp-10239/REGISTRY: Strategy: RoundRobinStrategy
[2021-03-18T11:49:41.640Z] INFO  mbp-10239/REGISTRY: Discoverer: LocalDiscoverer
[2021-03-18T11:49:41.641Z] INFO  mbp-10239/BROKER: Serializer: JSONSerializer
[2021-03-18T11:49:41.647Z] INFO  mbp-10239/BROKER: Validator: FastestValidator
[2021-03-18T11:49:41.649Z] INFO  mbp-10239/BROKER: Registered 13 internal middleware(s).
[2021-03-18T11:49:41.650Z] INFO  mbp-10239/BROKER: Transporter: NatsTransporter
[2021-03-18T11:49:41.659Z] INFO  mbp-10239/TRANSIT: Connecting to the transporter...
[2021-03-18T11:49:41.689Z] WARN  mbp-10239/TRANSIT: Connection is failed. client.on is not a function
[2021-03-18T11:49:46.691Z] INFO  mbp-10239/TRANSIT: Reconnecting...
[2021-03-18T11:49:46.692Z] WARN  mbp-10239/TRANSIT: Connection is failed. client.on is not a function
[2021-03-18T11:49:51.695Z] INFO  mbp-10239/TRANSIT: Reconnecting...
[2021-03-18T11:49:51.696Z] WARN  mbp-10239/TRANSIT: Connection is failed. client.on is not a function

I started NATS on my Mac in docker with this command (runs NATS server 2.2.0):

docker run -p 4222:4222 -p 8222:8222 -ti nats:latest

I can confirm NATS is working by using custom code to connect to NATS and check for connections through NATS web "gui":

const { connect } = require("nats");

connect({ port: 4222 }).then((nc: any) => {
  console.log('connected'); // prints connected
});

When I visit http://localhost:8222/connz it shows following info:

{
  "server_id": "NASOV2SRV6RBPZHUQ5BRXFBW3EALTK343V2EJVE7QKN6BH7LLVDTWMPS",
  "now": "2021-03-18T11:55:07.7447504Z",
  "num_connections": 1,
  "total": 1,
  "offset": 0,
  "limit": 1024,
  "connections": [
    {
      "cid": 3,
      "ip": "172.17.0.1",
      "port": 35014,
      "start": "2021-03-18T11:54:35.3804829Z",
      "last_activity": "2021-03-18T11:54:35.3860148Z",
      "rtt": "3.8161ms",
      "uptime": "32s",
      "idle": "32s",
      "pending_bytes": 0,
      "in_msgs": 0,
      "out_msgs": 0,
      "in_bytes": 0,
      "out_bytes": 0,
      "subscriptions": 0,
      "lang": "nats.js",
      "version": "2.0.0"
    }
  ]
}

So NATS is working and I am able to connect to it. Moleculer can not connect to NATS though for some reason.

   moleculer 0.14.12
   nats: 2.0.0
   node: v12.13.1
AndreMaz commented 3 years ago

UPDATE: I've managed to repro the issue. You're using nats.js v2.0 that was released 3 days ago and has a lot of breaking changes. At the moment Moleculer's NATS transporter works with the nats client ^1.4.12.


Hi @alekbarszczewski tested here the example that you've provided + nats server v2.2.0 and seems to be working fine. image

Can you provide a repro repo?

alekbarszczewski commented 3 years ago

Thanks! downgrading nats package solved the issue.

icebob commented 3 years ago

WIP: https://github.com/moleculerjs/moleculer/pull/942