Create a Chatroom
Building a Chatroom in aos
ao
, then that means we understand at least the basic methodology of sending and receiving messages. If not, it's suggested that you review the Messaging tutorial before proceeding.In this tutorial, we'll be building a chatroom within ao
using the Lua scripting language. The chatroom will feature two primary functions:
- Register: Allows processes to join the chatroom.
- Broadcast: Sends messages from one process to all registered participants.
Let's begin by setting up the foundation for our chatroom.
Video Tutorial
Step 1: The Foundation
- Open your preferred code editor.
- Create a new file named
chatroom.lua
.
Step 2: Creating The Member List
- In
chatroom.lua
, you'll begin by initializing a list to track participants:
Members = Members or {}
- Save the
chatroom.lua
file
Step 3: Load the Chatroom into aos
With chatroom.lua
saved, you'll now load the chatroom into aos
.
- If you haven't already, start your
aos
in your terminal inside the directory where chatroom.lua is saved - In the
aos
CLI, type the following script to incorporate your script into theaos
process:
.load chatroom.lua
As the screenshot above shows, you may receive undefined
as a response. This is expected, but we still want to make sure the file loaded correctly.
undefined
is a standard response, indicating that no result was returned. This can be observed when loading resources or executing operations. For instance, executing X = 1
will yield undefined
because the statement does not include a return statement.However, if you execute X = 1; return X
, the environment will return the value 1
. This behavior is essential to understand when working within this framework, as it helps clarify the distinction between executing commands that modify state versus those intended to produce a direct output.- Type
Members
, or whatever you named your user list, inaos
. It should return an empty array{ }
.
If you see an empty array, then your script has been successfully loaded into aos
.
Step 4: Creating Chatroom Functionalities
The Registration Handler
The register handler will allow processes to join the chatroom.
- Adding a Register Handler: Modify
chatroom.lua
to include a handler forMembers
to register to the chatroom with the following code:-- Modify `chatroom.lua` to include a handler for `Members` -- to register to the chatroom with the following code: Handlers.add( "Register", Handlers.utils.hasMatchingTag("Action", "Register"), function (msg) table.insert(Members, msg.From) Handlers.utils.reply("registered")(msg) end )
This handler will allow processes to register to the chatroom by responding to the tagAction = "Register"
. A printed message will confirm statingregistered
will appear when the registration is successful. - Reload and Test: Let's reload and test the script by registering ourselves to the chatroom.
- Save and reload the script in aos using
.load chatroom.lua
. - Check to see if the register handler loaded with the following script:
Handlers.list
This will return a list of all the handlers in the chatroom. Since this is most likely your first time developing inaos
, you should only see one handler with the nameRegister
.- Let's test the registration process by registering ourselves to the chatroom:
Send({ Target = ao.id, Action = "Register" })
If successful, you should see that there was amessage added to your outbox
and that you then see a new printed message that saysregistered
.- Finally, let's check to see if we were successfully added to the
Members
list:
Members
If successful, you'll now see your process ID in theMembers
list. - Save and reload the script in aos using
Adding a Broadcast Handler
Now that you have a chatroom, let's create a handler that will allow you to broadcast messages to all members of the chatroom.
- Add the following handler to the
chatroom.lua
file:Handlers.add( "Broadcast", Handlers.utils.hasMatchingTag("Action", "Broadcast"), function (msg) for _, recipient in ipairs(Members) do ao.send({Target = recipient, Data = msg.Data}) end Handlers.utils.reply("Broadcasted.")(msg) end )
This handler will allow you to broadcast messages to all members of the chatroom. - Let's test the broadcast handler by sending a message to the chatroom:
Send({Target = ao.id, Action = "Broadcast", Data = "Broadcasting My 1st Message" })
- If successful, you should see that there was a
message added to your outbox
and that you then see a new printed message that saysBroadcasting My 1st Message
because you are also a recipient of this message since you're a member of theMembers
chatroom.
- If successful, you should see that there was a
Step 5: Inviting Morpheus to the Chatroom
Now that you've successfully registered yourself to the chatroom, let's invite Morpheus to join us. To do this, we'll send an invite to him that will allow him to register to the chatroom.
Morpheus is an autonomous agent with a handler that will respond to the tag Action = "Join"
, in which will then have him use your Register
tag to register to the chatroom.
- Let's send Morpheus an invitation to join the chatroom:
Send({ Target = Morpheus, Action = "Join" })
- To confirm that Morpheus has joined the chatroom, check the
Members
list:Members
If successful, you'll receive a broadcasted message from Morpheus.
Step 6: Inviting Trinity to the Chatroom
Within this message, he'll give you Trinity's process ID and tell you to invite her to the chatroom.
Use the same processes to save her process ID as Trinity
and to invite her to the chatroom as you did with Morpheus.
If she successfully joins the chatroom, she'll then pose the next challenge to you, creating a token.
Engaging Others in the Chatroom
Onboarding Others
- Invite aos Users: Encourage other aos users to join your chatroom. They can register and participate in the broadcast.
- Provide Onboarding Instructions: Share a simple script with them for easy onboarding:
Hey, let's chat on aos! Join my chatroom by sending this command in your aos environment:
Send({ Target = [Your Process ID], Action = "Register" })
Then, you can broadcast messages using:
Send({Target = [Your Process ID], Action = "Broadcast", Data = "Your Message" })
Next Steps
Congratulations! You've successfully built a chatroom in ao
and have invited Morpheus to join you. You've also created a broadcast handler to send messages to all members of the chatroom.
Next, you'll continue to engage with Morpheus, but this time you'll be adding Trinity to the conversation. She will lead you through the next set of challenges. Good Luck!