Serializers

For transportation needs a serializer module which serialize & deserialize the transferred packets. If you don’t set serializer, the default is the JSON serializer.

Example

let { ServiceBroker } = require("moleculer");
let broker = new ServiceBroker({
nodeID: "server-1",
transporter: "NATS",
serializer: "ProtoBuf"
});

Built-in serializers

JSON serializer

This is the default serializer. Serialize the packets to JSON string and deserialize the received data to packet.

let broker = new ServiceBroker({
...
// serializer: new JSONSerializer() // don't need to set, because it is the default
});

Avro serializer

This is an Avro serializer.

let AvroSerializer = require("moleculer").Serializers.Avro;
let broker = new ServiceBroker({
...
serializer: new AvroSerializer()
});
// Shorthand with default settings
let broker = new ServiceBroker({
serializer: "Avro"
});
Dependencies

To use this serializer install the avsc module with npm install avsc --save command.

MsgPack serializer

This is an MsgPack serializer.

let MsgPackSerializer = require("moleculer").Serializers.MsgPack;
let broker = new ServiceBroker({
...
serializer: new MsgPackSerializer()
});
// Shorthand with default settings
let broker = new ServiceBroker({
serializer: "MsgPack"
});
Dependencies

To use this serializer install the msgpack5 module with npm install msgpack5 --save command.

ProtoBuf serializer

This is a Protocol Buffer serializer.

let ProtoBufSerializer = require("moleculer").Serializers.ProtoBuf;
let broker = new ServiceBroker({
...
serializer: new ProtoBufSerializer()
});
// Shorthand with default settings
let broker = new ServiceBroker({
serializer: "ProtoBuf"
});
Dependencies

To use this serializer install the protobufjs module with npm install protobufjs --save command.

Custom serializer

You can also create your custom serializer module. We recommend you that copy the source of JSONSerializer and implement the serialize and deserialize methods.