ipc

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 on all connected hosts 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
    

    Worker system bus

    For a single host worker queue can be used for system messages, all worker processes will be listening and process events the same was as in Redis case.

    ipc-system-queue=worker
    
Source:

Members

(inner, constant) args

Source:
Default Value:
  • [
      {
        "name": "ping",
        "obj": "ping",
        "type": "map",
        "merge": 1,
        "descr": "Keep alive pings for workers: interval:ms how oftern do pings, kill:ms kill worker after this period"
      },
      {
        "name": "system-queue",
        "descr": "System queue name to send broadcast control messages, this is a PUB/SUB queue to process system messages like restart, re-init config,..."
      }
    ]
Properties:
Name Type Description
args Array.<ConfigOptions>

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 : the app.id will 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 emit call 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, msg must 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

    If called inside the server, it process the message directly, reply is passed in the callback if given.

Source:
Parameters:
Name Type Attributes Description
op string
msg object | string
options object <optional>
Properties
Name Type Attributes Default Description
timeout int <optional>
30000

can be used to specify a timeout for how long to wait the reply, default is 5s to avoid hanging if a worker exists

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" })