moleculerjs / moleculer

:rocket: Progressive microservices framework for Node.js
https://moleculer.services/
MIT License
6.11k stars 580 forks source link

[Proposal] broker.broadcast update and broker.send #221

Closed ColonelBundy closed 1 year ago

ColonelBundy commented 6 years ago

UPDATED!

broker.broadcast

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']
});

broker.send

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?

icebob commented 6 years ago

Good idea, but maybe need a better method name. For me the publish doesn't mean this logic.

ColonelBundy commented 6 years ago

@icebob How about broker.emit instead?

quex46 commented 6 years ago

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
});
icebob commented 6 years ago

@quex46 Good idea. And it is flexible.

ColonelBundy commented 6 years ago

@quex46 right, forgot about that one. skipSelf sounds good!

icebob commented 6 years ago

Is there somebody to implement the first proposal?

icebob commented 6 years ago

@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"]
});
zllovesuki commented 6 years ago

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

icebob commented 6 years ago

@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.