Skip to content

Publish–subscribe messaging

okahilak edited this page Oct 18, 2021 · 2 revisions

Internally, InVesalius uses publish–subscribe messaging pattern for much of the communication between the modules.

A message consists of a topic and any number of optional key–value pairs. An example message:

Topic: Add marker
Content:

ball_id: 0
size: 2
colour: [1.0, 1.0, 0.0]
coord: [10.0, 20.0, 30.0]

In InVesalius code, sending this message would be done as follows:

from invesalius.pubsub import pub as Publisher

Publisher.sendMessage('Add marker', ball_id=0, size=2, colour=[1.0, 1.0, 0.0], coord=[10.0, 20.0, 30.0]

Communicating with InVesalius from an external process

InVesalius also supports sending and receiving messages from an external process.

To do that, the external process needs to run a Socket.IO server (see Socket.IO documentation).

Then, InVesalius is started with the --remote-host command line argument. For example:

python app.py --remote-host http://localhost:5000/

After that, all internal communication is sent to the Socket.IO server in from_neuronavigation topic.

The server can send a message to InVesalius in to_neuronavigation topic. The content of the message should consist of topic and data keys, where topic is string that determines the InVesalius's internal topic in which the message is published and data is a dictionary, consisting of the key–value pairs that comprise the message.

An example of how to send an "Add marker" message to InVesalius from the Socket.IO server, using python-socketio library:

import socketio

sio = socketio.Server()
...
sio.emit(
    event="to_neuronavigation",
    data={
        "topic": "Add marker",
        "data": {
            "ball_id": 0,
            "size": 2,
            "colour": [1.0, 1.0, 0.0],
            "coord": [10.0, 20.0, 30.0],
        },
    }