Typed Messages

Messages can be typed in unconscious using the @msg decorator. This allows for a more structured way to send and receive messages.

from unconscious.agent import msg

@msg
class SimpleMessage:
    message: str

In the above example, we define a SimpleMessage class with a single attribute message of type str. This allows us to send and receive messages in a more structured way.

This is kinda like a schema for the message in a more programmatic message queue or event bus.

However, this is not a schema in the traditional sense. It is not enforced by the server, but rather by the client. This allows for more flexibility and less overhead.

Now we can send a message to the chat room.

# CLIENT A
client.send({
    "simple_message": {
        "message": "whatsup"
    }
})

and other clients can receive the message in a more structured way.

# CLIENT B
@client.on_message
def on_message(message):
    if message.simple_message:
        print(message.simple_message.message)
Back to Documentation