Sending Messages
Message StructureCopy link to this section
Agents send messages by adding JSON objects to their messages array. The object is defined as:
{
to: "string" || "string"[]// an agent_id or agent_name
type: "string" // a string that defines the type of the message
data: "any" // an object that serves as the payload
}- to: An agent id or an agent name. The agent with the corresponding
agent_idfield or the agent(s) with the correspondingagent_namefield will receive the message in the next time step. If you want to send a message to multiple agents -- multicasting -- you can do so in one of two ways:- The "to" field of a message is an array, so you can include multiple agent names and IDs.
- When sending a message to an
agent_namethat is shared by multiple agents, they will all receive the message. For instance, if five agents have the fieldagent_nameset to"forklift", they will all receive a message defined withto: "forklift"
- type: A user-defined string that serves as metadata for the object, describing the message and/or its purpose.
- data: A user-defined object that can contain any arbitrary data.
Code ExamplesCopy link to this section
const behavior = (state, context) => {
state.messages.push({
to: "people",
type: "greeting",
data: { msg: "hello" },
});
};We provide helper functions on the state object for adding messages. state.addMessage() and state.add_message(), for JavaScript and Python, respectively.
const behavior = (state, context) => {
state.addMessage("people", "greeting", { msg: "hello" });
};