To enable communication between nodes (ServiceBrokers), you must configure a transporter. A transporter is Moleculer’s transport layer / messaging backend: it carries requests, responses and events between processes over a message broker (NATS, Redis, Kafka, MQTT, AMQP) or, with the TCP transporter, directly between nodes without any broker. The available transporters typically connect to a central message broker, facilitating reliable message exchange among remote nodes. These message brokers primarily support the publish/subscribe messaging pattern.
TerminologyIn these docs ServiceBroker / the broker (as in
broker.call()or “broker option”) means Moleculer’sServiceBrokerinstance running in your process; message broker means the external messaging server such as NATS or RabbitMQ that a transporter connects to.
Because the transporter is an abstraction, the same service code runs unchanged over any of them — you choose the broker for operational reasons (what you already run, throughput, persistence), not because your code depends on it.
Transporters
The transporter module in Moleculer plays a critical role in facilitating communication between services running on multiple nodes. It manages the transmission of events, request calls, and processing responses among nodes in the network. Note that the transporter only carries the packets; deciding which node receives a request is done by the ServiceBroker’s registry (client-side load balancing) before the packet is handed to the transporter. The exception is the disabled balancer mode, where the message broker’s own queue-group balancing is used.
Abstraction for Seamless Switching
The communication logic is abstracted away from the transporter class itself. This allows for seamless switching between different transporters without requiring any modifications to your service code. You can choose the most suitable transporter for your needs (e.g., TCP, NATS, Redis) without impacting your service logic, offering flexibility and future-proofing for your application.
TCP transporter
This is a no-dependency, zero-configuration TCP transporter. It uses Gossip protocol to disseminate node statuses, service list and heartbeats. It contains an integrated UDP discovery feature to detect new and disconnected nodes on the network.
If the UDP is prohibited on your network, use urls option. It is a list of remote endpoints (host/ip, port, nodeID). It can be a static list in your configuration or a file path which contains the list.
Use TCP transporter with default options
// moleculer.config.js |
All TCP transporter options with default values
// moleculer.config.js |
TCP transporter with static endpoint list
// moleculer.config.js |
You don’t need to set port because it find & parse the self TCP port from URL list.
TCP transporter with shorthand static endpoint list
It needs to start with tcp://.
// moleculer.config.js |
TCP transporter with static endpoint list file
// moleculer.config.js |
// nodes.json |
Serviceless nodePlease note, you don’t need to list all remote nodes. It’s enough at least one node which is online. For example, create a “serviceless” gossiper node, which does nothing, just shares other remote nodes addresses by gossip messages. So all nodes must know only the gossiper node address to be able to communicate with all other nodes.
NATS Transporter
Built-in transporter for NATS.
NATS Server is a simple, high performance open source messaging system for cloud-native applications, IoT messaging, and microservices architectures.
// moleculer.config.js |
DependenciesTo use this transporter install the
natsmodule withnpm install nats --savecommand.
Examples
Connect to ‘nats://localhost:4222’
// moleculer.config.js |
Connect to a remote NATS server
// moleculer.config.js |
Connect to a remote NATS server with auth
// moleculer.config.js |
Connect with options
// moleculer.config.js |
Connect with TLS
// moleculer.config.js |
Redis Transporter
Built-in transporter for Redis.
// moleculer.config.js |
DependenciesTo use this transporter install the
ioredismodule withnpm install ioredis --savecommand.
Examples
Connect with default settings
// moleculer.config.js |
Connect with connection string
// moleculer.config.js |
Connect to a secure Redis server
// moleculer.config.js |
Connect with options
// moleculer.config.js |
Connect to Redis cluster
// moleculer.config.js |
MQTT Transporter
Built-in transporter for MQTT protocol (e.g.: Mosquitto).
// moleculer.config.js |
DependenciesTo use this transporter install the
mqttmodule withnpm install mqtt --savecommand.
Examples
Connect with default settings
// moleculer.config.js |
Connect with connection string
// moleculer.config.js |
Connect to secure MQTT server
// moleculer.config.js |
Connect with options
// moleculer.config.js |
AMQP (0.9) Transporter
Built-in transporter for AMQP 0.9 protocol (e.g.: RabbitMQ).
// moleculer.config.js |
DependenciesTo use this transporter install the
amqplibmodule withnpm install amqplib --savecommand.
Transporter options
Options can be passed to amqp.connect() method.
Connect to ‘amqp://guest:guest@localhost:5672’
// moleculer.config.js |
Connect to a remote server
// moleculer.config.js |
Connect to a secure server
// moleculer.config.js |
Connect to a remote server with options & credentials
// moleculer.config.js |
AMQP 1.0 Transporter
Built-in transporter for AMQP 1.0 protocol (e.g.: ActiveMq or RabbitMQ + rabbitmq-amqp1.0 plugin).
Please note, it is an experimental transporter. Do not use it in production yet!
// moleculer.config.js |
DependenciesTo use this transporter install the
rhea-promisemodule withnpm install rhea-promise --savecommand.
Transporter options
Options can be passed to rhea.connection.open() method, the topics, the queues, and the messages themselves.
Connect to ‘amqp10://guest:guest@localhost:5672’
// moleculer.config.js |
Connect to a remote server
// moleculer.config.js |
Connect to a remote server with options & credentials
// moleculer.config.js |
Kafka Transporter
Built-in transporter for Kafka.
It is a simple implementation. It transfers Moleculer packets to consumers via pub/sub. There are not implemented offset, replay…etc features.
DependenciesTo use this transporter install the
@platformatic/kafkamodule withnpm install @platformatic/kafka --savecommand. Both@platformatic/kafka1.x (>=1.18.0) and 2.x are supported (2.x requires Moleculer >=0.15.2).
Connect to Kafka broker
// moleculer.config.js |
Connect to Kafka with custom options
// moleculer.config.js |
Custom transporter
Custom transporter module can be created. We recommend to copy the source of NatsTransporter and implement the connect, disconnect, subscribe and send methods.
Create custom transporter
const BaseTransporter = require("moleculer").Transporters.Base; |
Use custom transporter
// moleculer.config.js |
Disabled balancer
Some transporter servers have built-in balancer solution. E.g.: RabbitMQ, NATS. If you want to use the transporter balancer instead of Moleculer balancer, set the disableBalancer broker option to true.
Only the NATS, AMQP (0.9) and AMQP 1.0 transporters have a built-in balancer (hasBuiltInBalancer). If you set disableBalancer: true with any other transporter (TCP, Redis, MQTT, Kafka), the broker logs a warning (The ... has no built-in balancer. Broker balancer is ENABLED.) and forces disableBalancer back to false.
When the balancer is disabled, the broker no longer selects the target node: the registry strategies and the preferLocal option are not applied, the request is published to the message broker and its queue-group mechanism picks a consumer.
Example
// moleculer.config.js |
Please noteIf you disable the built-in Moleculer balancer, all requests & events will be transferred via transporter (including local requests). E.g. you have a local math service and you call
math.addlocally, the request will be sent via transporter.
Serialization
Transporter needs a serializer module which serializes & deserializes the transferred packets. The default serializer is the JSONSerializer but there are several built-in serializer.
Note that certain data types (e.g., Date, Map, BigInt) cannot be serialized with native JSON serializer. If you are working with this kind of data consider using JSONExt or Notepack serializers.
Example
// moleculer.config.js |
JSON serializer
This is the default serializer. It serializes the packets to JSON string and deserializes the received data to packet.
// moleculer.config.js |
JSON Extended serializer
We implemented a new JSON serializer which (unlike the native JSON serializer) is able to serializer Buffer, BigInt, Date, Map, Set and RegExp classes.
Example
// moleculer.config.js |
Custom extensions
You can extend the serializer with custom types.
Example to extend with a custom class serializing/deserializing
class MyClass { |
// moleculer.config.js |
MsgPack serializer
Built-in MsgPack serializer.
// moleculer.config.js |
DependenciesTo use this serializer install the
msgpack5module withnpm install msgpack5 --savecommand.
Notepack serializer
Built-in Notepack serializer.
// moleculer.config.js |
DependenciesTo use this serializer install the
notepackmodule withnpm install notepack.io --savecommand.
CBOR serializer
CBOR (cbor-x) is the fastest than any other serializers.
Example
// moleculer.config.js |
Custom serializer
Custom serializer module can be created. We recommend to copy the source of JSONSerializer and implement the serialize and deserialize methods.
Create custom serializer
const BaseSerializer = require("moleculer").Serializers.Base; |
Use custom serializer
// moleculer.config.js |