Play Strawberry Docs
Scripts

Inter-script communication

Exchange small, versioned messages with another script and handle both kinds of error.

Send a message

ipc::send_ipc_message(recipient, &bytes) sends bytes to the script with that name and waits for its answer. The result has two levels: the outer Err(()) means the message could not be delivered; Ok(Err(text)) is the other script's refusal; Ok(Ok(bytes)) is its answer.

rust
/// Asks the plugin "bank" for a player's balance. The message format is this pair's own:
/// here, the player's UUID as text in, the balance as text out.
pub fn balance_of(uuid: &str) -> Option<i64> {
    match ipc::send_ipc_message("bank", uuid.as_bytes()) {
        // The bank answered.
        Ok(Ok(reply)) => String::from_utf8(reply).ok()?.parse().ok(),
        // The bank refused the request (its own error text).
        Ok(Err(_reason)) => None,
        // The message could not be delivered (no plugin of that name, or it is not running).
        Err(()) => None,
    }
}

Answer messages

Implement Plugin::handle_ipc_message(&self, sender, message), which returns Result<Vec<u8>, String>. The default answer refuses every message. Define a versioned format and check the sender, the length and every field before you act.

rust
impl Plugin for IpcDemo {
    fn new() -> Self {
        Self
    }

    fn metadata(&self) -> PluginMetadata {
        PluginMetadata {
            name: "bank".into(),
            version: env!("CARGO_PKG_VERSION").into(),
            authors: vec!["Your name".into()],
            description: "Answers balance requests from other plugins.".into(),
            dependencies: Vec::new(),
            permissions: Vec::new(),
        }
    }

    fn handle_ipc_message(&self, sender: String, message: Vec<u8>) -> Result<Vec<u8>, String> {
        if message.len() > 64 {
            return Err(format!("request from {sender} is too long"));
        }
        let uuid = String::from_utf8(message).map_err(|_| "not text".to_string())?;
        // Look the balance up in your own state; 0 keeps the example short.
        let balance: i64 = if uuid.is_empty() { 0 } else { 0 };
        Ok(balance.to_string().into_bytes())
    }
}
Please note

A message waits for the answer. Never send one while you hold a lock, and avoid two scripts asking each other at the same time: the server refuses the call that would make two scripts wait for each other, and that call fails. A message is not a permission.

Post without waiting

ipc::post(recipient, bytes) posts a message and returns at once with a job. A slow recipient holds only itself: your own tick never waits for it, also when you post from a sync task.

  • The message waits in the recipient's queue (64 messages, each at most 1 MiB) and is handled on the recipient's turn, in the order posted.
  • Poll the job later to read the answer, or drop it when the answer does not matter. The message is still delivered.
  • A post is refused when the recipient is not loaded or is your own script, the message is over 1 MiB, the queue is full, or your script is answering a module call.
  • If the recipient is unloaded or reloaded, every message still in its queue is answered with an error.

Know who sent a message

The sender is always the name the sending script is loaded under, filled in by the server, so you can keep what another script registers with you under that name.

The extension event plugin-unloaded tells you when a script was unloaded. Every message it sent reaches you before the event and none comes after it: drop what it registered there. A reload gives plugin-disable, plugin-unloaded, then plugin-enable.

Bound and recover

Limit message size and the work per request. Give one-off actions a request id so that a repeated message does nothing twice. Handle a missing or unloaded recipient without an endless retry queue.

On this page