Python
- Setup your app folder
mkdir reboot-chat-room && cd reboot-chat-room - Install Reboot
python3 -m venv ./.venv && source .venv/bin/activate && pip install reboot pydantic - Make sure Docker is running:
docker ps - Create a Pydantic
.pyfilemkdir -p ./api/chat_room/v1 && touch ./api/chat_room/v1/chat_room.py - Define your durable state data types and operations
# ./api/chat_room/v1/chat_room.py
from pydantic import BaseModel
from reboot.api import (
API,
Field,
Methods,
Reader,
Writer,
StateModel,
Type,
)
from typing import Optional
class ChatRoomState(StateModel):
messages: Optional[list[str]] = Field(tag=1)
class MessagesResponse(BaseModel):
messages: list[str] = Field(tag=1)
class SendRequest(BaseModel):
message: str = Field(tag=1)
ChatRoomMethods = Methods(
messages=Reader(
request=None,
response=MessagesResponse,
),
send=Writer(
request=SendRequest,
response=None,
),
)
api = API(
ChatRoom=Type(
state=ChatRoomState,
methods=ChatRoomMethods,
),
) - Create
.pyimplementation filemkdir -p ./backend/src && touch ./backend/src/chat_room_servicer.py - Implement
ChatServicer# ./backend/src/chat_room_servicer.py
from chat_room.v1.chat_room import (
MessagesResponse,
SendRequest,
)
from chat_room.v1.chat_room_rbt import ChatRoom
from rebootdev.aio.contexts import ReaderContext, WriterContext
class ChatRoomServicer(ChatRoom.Servicer):
async def messages(
self,
context: ReaderContext,
) -> MessagesResponse:
return MessagesResponse(messages=self.state.messages or [])
async def send(
self,
context: WriterContext,
request: SendRequest,
) -> None:
if not self.state.messages:
self.state.messages = []
self.state.messages.append(request.message) - Create a
mainentrypointtouch ./backend/src/main.py - Implement
main.py# ./backend/src/main.py
import asyncio
from chat_room.v1.chat_room_rbt import ChatRoom
from chat_room_servicer import ChatRoomServicer
from reboot.aio.applications import Application
from rebootdev.aio.external import InitializeContext
async def initialize(context: InitializeContext):
chat_room = ChatRoom.ref('reboot-chat-room')
await chat_room.send(
context,
message='Hello, World!',
)
async def main():
application = Application(
servicers=[ChatRoomServicer],
initialize=initialize,
)
await application.run()
if __name__ == '__main__':
asyncio.run(main()) - Create an
.rbtrcfiletouch .rbtrc - Add necessary flags to your
.rbtrcfilegenerate api/
generate --python=backend/api/
dev run --name=chat-room
dev run --watch=backend/src/**/*.py
dev run --python
dev run --application=backend/src/main.py - Run your app
rbt dev run - Query your app
curl -XPOST http://localhost:9991/chat_room.v1.ChatRoomMethods/Messages \
-H "x-reboot-state-ref:chat_room.v1.ChatRoom:reboot-chat-room"
Next steps
Well done! You made it!
This example is somewhat contrived as we only created one instance of
your ChatRoom data type, but in practice you are free to make as many
of these as you like!
Check out these links to continue your exploration:
- Build a React frontend - create a React frontend that connects to your backend and displays your messages in real-time.
- Learn about transactions - so far,
we've covered
readerandwritermethods, but Reboot's power extends totransactions. Learn abouttransactionsand all of the other method kinds here. - Join us on Discord - to ask questions and let us know what you think!
Hopefully, your wheels are starting to spin thinking of all of the possibilities!