Ctx emit balanced events send balanced events with broker function
event, the second parameter is the payload.
To send multiple values, wrap them into an Object.
The broadcast event is sent to all available local & remote services. It is not balanced, all service instances will receive it.
Send broadcast events with broker.broadcast method.
Subscribe to events
The v0.14 version supports Context-based event handlers. Event context is useful if you are using event-driven architecture and want to trace your events. If you are familiar with Action Context you will feel at home. The Event Context is very similar to Action Context, except for a few new event related properties.Legacy event handlers
• | ||
---|---|---|
• |
module.exports ={
name:"accounts",
events:{
"user.created"(ctx){
console.log("Payload:", ctx.params);
console.log("Sender:", ctx.nodeID);
console.log("Metadata:", ctx.meta);
console.log("The called event name:", ctx.eventName);ctx.emit("accounts.created",{ user: ctx.params.user });
}, |
|
---|
// Subscribe to all `user` events, e.g. "user.created", or "user.removed" "user.*"(ctx){
console.log("User event:", ctx.params);
}
// Subscribe to every events
// Legacy event handler signature with context
"**"(payload, sender,event, ctx){
}}}}; |
|
---|
The validation errors are not sent back to the caller, they are logged or you can catch them with the new global error handler.
$circuit-breaker.opened
The broker sends this event when the circuit breaker module change its state to open.Payload
|
|
|
|
|
|
|
|
---|---|---|
|
|
|
|
|
---|---|---|
Payload
$broker.error
The broker emits this event when an error occurs in the broker.Eventpayload
{"error": "<the error object with all properties>"
"module": "broker"// Name of the module where the error happened
} |
---|
Eventpayload
{
"error": "<the error object with all properties>"
"module": "transit"// Name of the module where the error happened
"type": "error-type"// Type of error. Full of error types: https://github.com/moleculerjs/moleculer/blob/master/src/constants.js }