module:ipc
- Description:
IPC communications between processes and support for subscriptions via queues.
The module is EventEmitter and emits messages received.
A special system queue can be configured and it will be used by all processes to listen for messages on the channel bkjs:role, where the role is the process role, the same messages that are processed by the server/worker message handlers like api:restart, config:init,....
All instances will be listening and processing these messages at once, the most usefull use case is refreshing the DB config on demand or restarting without configuring any other means like SSH, keys....
Redis system bus
If configured all processes subscribe to it and listen for system messages. Websockets in the API server also use the system bus to send broadcasts between multiple api instances.
queue-system=redis:// ipc-system-queue=system
- Source:
Methods
(static) broadcast(channel, msg, optionsopt, callbackopt)
- Description:
Send a message to a channel, this is high level routine that uses the corresponding queue, it uses eventually queue.publish. If no client or queue is provided in the options it uses default
systemQueue. If the channel starts with : theapp.idwill be prepended automatically.
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
channel |
string | ||
msg |
object | string | ||
options |
object |
<optional> |
|
callback |
function |
<optional> |
Example
ipc.broadcast(":worker", "worker:restart")
(static) emitMsg(op, msg, optionsopt)
- Description:
Wrapper around EventEmitter
emitcall to send unified IPC messages in the same format, this is for in-process mesaging only between modules.
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
op |
string | ||
msg |
object | string | ||
options |
object |
<optional> |
Examples
mymod.js
const { ipc } = require("backendjs");
...
ipc.on("user:subscribed", (msg) => {
console.log(msg);
});
othermod.js
const { api, ipc } = require("backendjs");
...
api.users.add({ ... }, (err, user) => {
if (!err) ipc.emitMsg("user.subscribed", user);
})
(static) initServer(options)
- Description:
This function is called by a server server process to setup IPC channels and support for cache and messaging
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
options |
object |
(static) initWorker(options)
- Description:
This function is called by a worker process to setup IPC channels and support for cache and messaging
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
options |
object |
(static) newMsg(op, msg, optionsopt)
- Description:
Returns an IPC message object,
msgmust be an object if given.
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
op |
string | ||
msg |
object | string | ||
options |
object |
<optional> |
(static) sendMsg(op, msg, optionsopt, callbackopt)
- Description:
Send a message to the server process via IPC messages, callback is used for commands that return value back
- the
timeoutproperty can be used to specify a timeout for how long to wait the reply, if not given the default is used - the rest of the properties are optional and depend on the operation.
If called inside the server, it process the message directly, reply is passed in the callback if given.
- the
- Source:
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
op |
string | ||
msg |
object | string | ||
options |
object |
<optional> |
|
callback |
function |
<optional> |
Example
ipc.sendMsg("op1", { data: "data" }, { timeout: 100 })
ipc.sendMsg("op1", { name: "name", value: "data" }, (data) => { console.log(data); })
ipc.sendMsg("op1", { 1: 1, 2: 2 }, { timeout: 100 })
ipc.sendMsg("op1", { 1: 1, 2: 2 }, (data) => { console.log(data); })
ipc.newMsg({ __op: "op1", name: "test" })