PROTOCOL.md

January 31, 2018 · View on GitHub

title: Protocol v3

This documentation describes the communication protocol between Moleculer nodes.

Variables in topic names:

  • <namespace> - Namespace from broker options
  • <nodeID> - Target nodeID
  • <action> - Action name. E.g.: posts.find
  • <group> - Event group name. E.g.: users
  • <event> - Event name. E.g.: user.created

Subscriptions

After the client is connected to the message broker (NATS, Redis, MQTT), it subscribes to the following topics:

TypeTopic name
EventMOL.EVENT.<nodeID>
Event (balanced)MOL.EVENTB.<event>
RequestMOL.REQ.<nodeID>
Request (balanced)MOL.REQB.<action>
ResponseMOL.RES.<nodeID>
DiscoverMOL.DISCOVER
Discover (targetted)MOL.DISCOVER.<nodeID>
InfoMOL.INFO
Info (targetted)MOL.INFO.<nodeID>
HeartbeatMOL.HEARTBEAT
PingMOL.PING
Ping (targetted)MOL.PING.<nodeID>
PongMOL.PONG.<nodeID>
DisconnectMOL.DISCONNECT

If namespace is defined, the topic prefix is MOL-namespace instead of MOL. For example: MOL-dev.EVENT if the namespace is dev.

Discovering

After subscriptions, the client broadcasts a DISCOVER packet. In response to this, all connected nodes send back INFO packet to the sender node. From these responses, the client builds its own service registry. At last, the client broadcasts own INFO packet to all other nodes.

Heartbeat

The client has to broadcast HEARTBEAT packets periodically. The period value comes from broker options (heartbeatInterval). The default value is 5 secs. If the client does not receive HEARTBEAT for heartbeatTimeout seconds from a node, marks it broken and doesn't route requests to this node.

Request-reply

When you call the broker.call method, the broker sends a REQUEST packet to the targetted node. It processes the request and sends back a RESPONSE packet to the requester node.

Event

When you call the broker.emit method, the broker sends an EVENT packet to the subscriber nodes. The broker groups & balances the subscribers, so only one instance per service receives the event. If you call the broker.broadcast method, the broker sends an ĘVENT packet to all subscriber nodes. It doesn't group & balance the subscribers.

Ping-pong

When you call the broker.transit.sendPing method, the broker sends a PING packet to the targetted node. If node is not defined, it sends to all nodes. If the client receives the PING packet, sends back a PONG response packet. If it receives, broker broadcasts a local $node.pong event to the local services.

Disconnect

When a node is stopping, it broadcasts a DISCONNECT packet to all nodes.

Packets

DISCOVER

Topic name:

  • MOL.DISCOVER (if broadcasts)
  • MOL.DISCOVER.node-1 (if sent only to node-1)
  • MOL-dev.DISCOVER (if namespace is dev)

Fields:

FieldTypeRequiredDescription
verstringProtocol version. Current: '3'.
senderstringSender nodeID.

INFO

Topic name:

  • MOL.INFO (if broadcasts)
  • MOL.INFO.node-1 (if sent only to node-1)
  • MOL-dev.INFO (if namespace is dev)

Fields:

FieldTypeRequiredDescription
verstringProtocol version. Current: '3'.
senderstringSender nodeID.
servicesobjectServices list. (*)
configobjectClient configuration. (*)
ipList[string]IP address list of node
hostnamestringHostname of node
clientobjectClient information
client.typestringType of client implementation(nodejs, java, go)
client.versionstringClient (Moleculer) version
client.langVersionstringNodeJS/Java/Go version

(*) In case of ProtoBuf, Avro or any other schema-based serializer, the field value is encoded to JSON string.

HEARTBEAT

Topic name:

  • MOL.HEARTBEAT
  • MOL-dev.HEARTBEAT (if namespace is dev)

Fields:

FieldTypeRequiredDescription
verstringProtocol version. Current: '3'.
senderstringSender nodeID.
cpudoubleCurrent CPU utilization (percentage).

REQUEST

Topic name:

  • MOL.REQ.node-2
  • MOL.REQB.<action> (if built-in balancer is disabled)
  • MOL-dev.REQ.node-2 (if namespace is dev)

Fields:

FieldTypeRequiredDescription
verstringProtocol version. Current: '3'.
senderstringSender nodeID.
idstringContext ID.
actionstringAction name. E.g.: posts.find
paramsobjectctx.params object. (*)
metaobjectctx.meta object. (*)
timeoutdoubleRequest timeout (distributed) in milliseconds.
levelint32Level of request.
metricsbooleanNeed to send metrics events.
parentIDstringParent context ID.
requestIDstringRequest ID from ctx.requestID.

(*) In case of ProtoBuf, Avro or any other schema-based serializer, the field value is encoded to JSON string.

RESPONSE

Topic name:

  • MOL.RES.node-1
  • MOL-dev.RES.node-1 (if namespace is dev)

Fields:

FieldTypeRequiredDescription
verstringProtocol version. Current: '3'.
senderstringSender nodeID.
idstringContext ID (from REQUEST).
successbooleanIs it a success response?
dataobjectResponse data if success. (*)
errorobjectError object if not success. (*)
metaobjectctx.meta object. (*)

(*) In case of ProtoBuf, Avro or any other schema-based serializer, the field value is encoded to JSON string.

EVENT

Topic name:

  • MOL.EVENT.node-1
  • MOL.EVENTB.<group>.<event> (if built-in balancer is disabled)
  • MOL-dev.EVENT.node-1 (if namespace is dev)

Fields:

FieldTypeRequiredDescription
verstringProtocol version. Current: '3'.
senderstringSender nodeID.
eventstringEvent name. E.g.: users.created
dataobjectEvent payload. (*)
groupsArray<string>Groups for balanced events. If null or empty, the event is broadcasted.
broadcastbooleanIs it a broadcast message?

(*) In case of ProtoBuf, Avro or any other schema-based serializer, the field value is encoded to JSON string.

PING

Topic name:

  • MOL.PING (if broadcasts)
  • MOL.PING.node-1 (if sent only to node-1)
  • MOL-dev.PING (if namespace is dev)

Fields:

FieldTypeRequiredDescription
verstringProtocol version. Current: '3'.
senderstringSender nodeID.
timeint64Time of sent. (*)

(*) The number of milliseconds between 1 January 1970 00:00:00 UTC and the given date.

PONG

Topic name:

  • MOL.PONG.node-1
  • MOL-dev.PONG (if namespace is dev)

Fields:

FieldTypeRequiredDescription
verstringProtocol version. Current: '3'.
senderstringSender nodeID.
timeint64Timestamp of sent. (*)
arrivedint64Timestamp of arrived. (*)

(*) The number of milliseconds between 1 January 1970 00:00:00 UTC and the given date.

DISCONNECT

Topic name:

  • MOL.DISCONNECT
  • MOL-dev.DISCONNECT (if namespace is dev)

Fields:

FieldTypeRequiredDescription
verstringProtocol version. Current: '3'.
senderstringSender nodeID.