A proposal to modify broker.broadcast method, to allow a node to broadcast to all nodes except itself. This can be done adding a key called skipSelf
Example: (thanks, @quex46)
broker.broadcast('event', {}, {
skipSelf: true // false by default
group: ['one', 'two']
});
To further extend the functionality of the event system, how about a broker.send as well? it would take an event name, data, nodeID and optionally a group. This would allow a node to reply to another node, reasoning by keeping the group would be to better support mixin isolation.
Example:
broker.send('event', {}, {
id: 'node-123', // Node to send to.
group: ['one', 'two'] // (optional)
});
UPDATED
broker.broadcast("user.created", { user }, {
nodeID: "node-123",
groups: ["user", "purchase"]
});
Thoughts?
Good idea, but maybe need a better method name. For me the publish doesn't mean this logic.
@icebob How about broker.emit instead?
broker.emit is already in use. Maybe just extend options (third parameter) for broker.broadcast by adding something like this:
broker.broadcast("user.created", { user }, {
groups: ["user", "purchase"],
skipSelf: true
});
@quex46 Good idea. And it is flexible.
@quex46 right, forgot about that one. skipSelf sounds good!
Is there somebody to implement the first proposal?
@ColonelBundy for 2nd proposal: I think no need a new function. It can be merged to the first proposal.
E.g:
broker.broadcast("user.created", { user }, {
nodeID: "node-123",
groups: ["user", "purchase"]
});
I was under the impression that the current code already skips the current node: https://github.com/moleculerjs/moleculer/blob/f6ac90bcd128ded3bacd8220e1ddae97b0e0ce6e/src/service-broker.js#L1288
@zllovesuki the mentioned code is applied to only remote nodes. It gathers the ID of remote nodes, so skips the local nodeID and here sends to all local nodes.
Most helpful comment
broker.emitis already in use. Maybe just extend options (third parameter) forbroker.broadcastby adding something like this: