Skip to main content

Python

  1. Setup your app folder
    mkdir reboot-chat-room && cd reboot-chat-room
  2. Install Reboot
    python3 -m venv ./.venv && source .venv/bin/activate && pip install reboot pydantic
  3. Make sure Docker is running:
    docker ps
  4. Create a Pydantic .py file
    mkdir -p ./api/chat_room/v1 && touch ./api/chat_room/v1/chat_room.py
  5. 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,
    ),
    )
  6. Create .py implementation file
    mkdir -p ./backend/src && touch ./backend/src/chat_room_servicer.py
  7. 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)
  8. Create a main entrypoint
    touch ./backend/src/main.py
  9. 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())
  10. Create an .rbtrc file
    touch .rbtrc
  11. Add necessary flags to your .rbtrc file
    generate 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
  12. Run your app
    rbt dev run
  13. 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:

Hopefully, your wheels are starting to spin thinking of all of the possibilities!