Skip to main content

Liaison

Liaison provides a way of interfacing with a Python backend by sending commands as JSON.

History

Liaison was built by Open Science Tools Ltd. to provide an interface between the PsychoPy Studio app (a frontend built in Electron/Svelte) and the PsychoPy Python library. Owing to the general usefulness of being able to interface with Python via JSON commands, we decided to release it as its own package, for general use. Liaison is distributed under an MIT License.

While Liaison is designed to allow support for other communication methods to be added in future, and could hypothetically support any method over which a JSON string can be sent, it presently only supports sending/receiving messages over websocket as this is the implementation it was built for.

Liaison is not a generic data structure tool. It's a system for sending simple instructions to a Python instance and doing some basic namespace management. If you are interested in the exchange of serialised strcutured data, check out Google's Protocol Buffers or Cap'n Proto.

Getting started with Websocket Liaison

Installing

To use Liaison with websockets, you will need to install with the optional websocket dependencies:

pip install liaison-backend[websocket]

Then make sure you have something setup on your application frontend which can make command line calls, such as Node's child_process module.

Getting constants

Liaison uses a few constant values to communicate when it's started and stopped. While you could hardcode these, you can also get them from Liaison via command line:

python -m liaison.constants

which will return a JSON string:

{"START_MARKER": "LIAISON:CONNECTED", "STOP_MARKER": "LIAISON:DISCONNECTED"}

When Liaison starts, it will send the START_MARKER, and will send the STOP_MARKER when it stops, so looking out for these values lets you keep track of the life cycle of the Liaison backend.

Starting Liaison

To start a Liaison backend on a particular websocket address, just call:

python -m liaison.websocket <address>

This will run continuously until told to stop, listening for messages on the given websocket address. It will send the START_MARKER constant to its stdout upon starting, and will send the STOP_MARKER constant upon stopping.

Example: Starting a Liaison backend from a Node (JavaScript) app

import proc from "child_process";

// binary decoder for websocket messages
const decoder = new TextDecoder();
// choose an address to open the websocket on
const address = "localhost:8003";
// get liaison constants
const liaisonConstants = JSON.parse(
    proc.execSync(
        "python -m liaison.constants"
    )
);
// start process running liaison
const liaisonProcess = proc.spawn(
    `python -m liaison.websocket ${address}`
);
// wait for started message
var liaisonStarted = Promise.withResolvers()
liaisonProcess.stdout.on("data", evt => {
    // if message indicates liaison has started, resolve promise
    if (decoder.decode(evt) === `${liaisonConstants.START_MARKER}@${address}`) {
        liaisonStarted.resolve(evt)
    }
})
await liaisonStarted.promise
// create websoket to send messages over
let liaisonSocket = new WebSocket(`ws://${address}`);

Companion object

Each instance of Liaison creates an instance of Companion; this is an object with its own namespace, whose methods Liaison can call, giving access to Python through them. Objects in the Companion's namespace can be used as an argument to any given function by prepending them with $. Companion objects have the following methods:

get

Get the value from this Companion's namespace which a string points to.

Parameters
  • target <string> A string of attribute/key references starting with a name in this Companion's namespace or an importable package.
Returns
  • <any> The value of the given target

exists

Check whether a target exists, either as a name in this Companion's namespace or as part of an installed Python module.

Parameters
  • target <string> A string of attribute/key references starting with a name in this Companion's namespace or an importable package.
Returns
  • <boolean> true/false according to whether the target exists

init

Initialize an object and store it in this Companion's namespace

Parameters
  • name <string> Name to store the created object by
  • cls <string> Resolvable string pointing to the class to initialise (or a callable to use as a constructor)
  • args <array> Positional arguments to supply to the constructor
  • kwargs <object> Named arguments to supply to the constructor
Returns
  • <string> The name the object was registered to

run

Call the given function

Parameters
  • fcn <string> Resolvable path to the function
  • args <array> Positional arguments to pass to the function
  • kwargs <object> Named arguments to pass to the function
Returns
  • <any> Output from the function

try

The same as run, but called within a try:except statement

Parameters
  • fcn <string> Resolvable path to the function
  • args <array> Positional arguments to pass to the function
  • kwargs <object> Named arguments to pass to the function
Returns
  • <object> Object with a key "success" indicating whether the method executed successfully, and a key "result" containing either the output of the function or the caught error

register

Register something resolvable (e.g. a module, class or method) under a particular name

Parameters
  • name <string> Name to register as
  • target <string> Resolvable string pointing to object to register
Returns
  • <string> Name the object was registered as

store

Store an arbitrary value in this Companion's namespace

Parameters
  • name <string> Name to register as
  • value <string> Value to store
Returns
  • <string> Name the value was registered as

ping

Returns the word "pong". Useful for testing whether a Liaison is still alive, or send routine messages to keep a connection open.

Sending messages

Once you have both a Liaison process and a websocket setup, you can send messages to Liaison which will be executed by its Companion object. Liason has access to any Python modules present in the python environment it was executed from. The syntax of these messages is as follows:

  • command <object>
    • command <string> Companion method to execute, see above for options and inputs for each
    • args <array> Array of positional arguments to pass to the Companion method
    • kwargs <object> Object of key:value pairs indicating the keyword arguments to be passed to the Companion method
  • id <string> An arbitrary ID; when the function returns, any output will be sent back as a websocket message with the same ID

Example: Performing a T-Test in scipy over Liaison

// define some data to do a T-Test on
let a = [0.5982758 , 0.67019733, 0.68617796, 0.28764011]
let b = [0.41138649, 1.67023998, 1.0420896 , 1.49671732]
// create the command to send
let cmd = {
    command: {
        command: 'run',
        args: [
            "scipy.stats:ttest_ind", a, b
        ],
        kwargs: {
            equal_var: false
        }
    },
    id: crypto.randomUUID()
}
// send the command as a JSON string
liaisonSocket.send(
    JSON.stringify(cmd)
)
// wait for a reply (optional)
let replied = Promise.withResolvers()
let lsnr = evt => {
    // parse message
    let data = JSON.parse(evt.data)
    // if message is a reply to our command...
    if (data.evt.id !== cmd.id) {
        // stop listening
        liaisonSocket.removeEventListener("message", lsnr)
        // resolve/reject promise
        if (data.response) {
            replied.resolve(data.response)
        } else {
            replied.reject(data)
        }
    }    
}
liaisonSocket.addEventListener("message", lsnr)
// get response
let resp = await replied.promise

Metadata

Release files for liaison-py 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for liaison-py 1.0.1
File Size Uploaded
liaison_py-1.0.1.tar.gz 12.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for liaison-py 1.0.1
File Interpreter ABI Platform
liaison_py-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 23.7 kB

Release files / liaison_py-1.0.1.tar.gz

Download URL liaison_py-1.0.1.tar.gz
Size 12.4 kB
Tags Source
SHA-256 checksum
How to use checksums
3a232933304a8b9f9e86bacf2b342856aea6130791e1fd9ba6a52e936156770c
BLAKE2b-256 checksum
How to use checksums
421c729b1caa46ce1dfd565f2f82e41c9ed983f6a7990fa03cef777e533d24b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 18, 2026.

Transparency log

Release files / liaison_py-1.0.1-py3-none-any.whl

Download URL liaison_py-1.0.1-py3-none-any.whl
Size 11.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5899a9324e58f4bc7d8ae7597348d4c793aee0a496a63da661d3f1ab06674fb8
BLAKE2b-256 checksum
How to use checksums
4dc93a853c8a7374f42415858b43dc35ce1f20108d456edbd5d897e5deea4eb0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 18, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page