MojoScale Studio Docs
API Reference

mojoscale_messaging

MojoScale device channel messaging.

Studio Docs Networking

import mojoscale_messaging

The mojoscale_messaging module exposes device-scoped realtime channels. MQTT topics, broker credentials, and internal Studio update topics remain managed by MojoScale.

Quick example

import mojoscale_messaging

temperature = mojoscale_messaging.channel("temperature")

def on_ready():
    temperature.send({"temp": 23.4, "unit": "C"})

def on_message(message):
    print(message)

mojoscale_messaging.on_connect(on_ready)
mojoscale_messaging.connect()
temperature.subscribe(on_message)

# Equivalent lower-level form if you prefer module functions:
mojoscale_messaging.send(temperature, {"temp": 23.4})

importmojoscale_messaging

MojoScale device channel messaging.

The mojoscale_messaging module exposes device-scoped realtime channels. MQTT topics, broker credentials, and internal Studio update topics remain managed by MojoScale.

API 8 available

mojoscale_messaging.channel(name: str) -> channel

Create a device-scoped messaging channel handle.

Parameters
Name Type Pass as Required Description
name str positional or keyword Yes Channel name. Use letters, numbers, underscores, hyphens, or dots. Names starting with "__studio__" are reserved for Studio libraries.
Returns

channel Messaging channel handle. Use handle.send(message_payload) and handle.subscribe(callback) in Studio code.

mojoscale_messaging.connect() -> bool

Connect to Studio-managed MojoScale Messaging.

The device uses the managed credentials flashed by Studio. Register on_connect(callback) before connect() when you want to publish only after the broker connection is ready.

Returns

bool True if the managed MQTT connection was started or already active.

mojoscale_messaging.connected() -> bool

Check whether Studio-managed MojoScale Messaging is connected.

Returns

bool True when the managed messaging connection is active.

mojoscale_messaging.on_connect(callback: callback) -> bool

Register a callback for MojoScale Messaging connection.

Parameters
Name Type Pass as Required Description
callback callback positional or keyword Yes Named global function to run when Studio-managed messaging connects. Pass the function without quotes; Studio sends its name string to firmware.
Returns

bool True if the callback was registered.

mojoscale_messaging.on_disconnect(callback: callback) -> bool

Register a callback for MojoScale Messaging disconnection.

Parameters
Name Type Pass as Required Description
callback callback positional or keyword Yes Named global function to run when Studio-managed messaging disconnects. Pass the function without quotes; Studio sends its name string to firmware.
Returns

bool True if the callback was registered.

mojoscale_messaging.send(channel: channel, message_payload: list) -> bool

Send a realtime message on a channel.

Users normally call this as handle.send(message_payload). Studio rewrites that friendly channel-handle form to the native firmware call. The module-level form is also valid as mojoscale_messaging.send(handle, message_payload).

Parameters
Name Type Pass as Required Description
channel channel positional or keyword Yes Messaging channel handle returned by mojoscale_messaging.channel(name).
message_payload list positional or keyword Yes Message to send. Strings, numbers, booleans, lists, and dicts are accepted. Literal dict/list payloads are serialized to JSON by Studio.
Returns

bool True if the message was accepted for publish.

mojoscale_messaging.subscribe(channel: channel, callback: callback) -> bool

Receive realtime messages from a channel.

Users normally call this as handle.subscribe(callback). Studio rewrites that friendly channel-handle form to the native firmware call. The module-level form is also valid as mojoscale_messaging.subscribe(handle, callback). The callback receives one message argument.

Parameters
Name Type Pass as Required Description
channel channel positional or keyword Yes Messaging channel handle returned by mojoscale_messaging.channel(name).
callback callback positional or keyword Yes Named global function to run for each inbound message. Pass the function without quotes; Studio sends its name string to firmware.
Returns

bool True if the subscription was registered.

mojoscale_messaging.turn_off_wireless_update() -> bool

Disable Studio wireless update commands for this script.

Importing mojoscale_messaging automatically enables Studio-managed messaging and wireless script update support. Call this if the script only needs user channels and should not listen for Studio update commands.

Returns

bool True when the internal update listener was disabled.