r/FastAPI

▲ 24 r/FastAPI+1 crossposts

Composable, reusable WebSocket components for any ASGI framework (Django, FastAPI, Litestar)

Hi all, I'm the maintainer of a small channels (WebSocket) extension library for Django (and FastAPI too). While using and maintaining it, I started thinking it could become a small framework as well: composable and framework-independent, so it could be reused across Django/FastAPI/Litestar/... as long as the framework supports ASGI. Before going further, I'm putting the blueprint out here to compare notes with people who work with WebSockets regularly. If you have ever worked with WebSockets, I hope you can share any ideas, info, pain points, or suggestions you have.

Prerequisites, what my library already has:

  • Function-like handlers rather than while True + if/else
  • Automatic AsyncAPI doc generation
  • Full type hints
  • A testing kit
  • Support for all ASGI-based frameworks (Django, FastAPI, ...)

At a glance, it looks like this:

@ws_handler(output_type=ChatNotificationMessage)
async def handle_chat(self, message: ChatMessage) -> None:
    # Automatically routed, validated, and type-safe
    await self.broadcast_message(
        ChatNotificationMessage(payload=message.payload)
    )

@ws_handler
async def handle_ping(self, message: PingMessage) -> PongMessage:
    return PongMessage()  # Auto-documented in AsyncAPI

If you have ever worked with WebSockets, I think you get the idea of what it does here.

Recently I added the Topic feature, which is composable and reusable. It came out of a multiplexing feature request, and I was inspired by Phoenix Channels. It looks something like this:

class DiscussionTopic(Topic):
    pattern = "discussion:{pk}"

    async def authorize(self, pk: str) -> bool:
        return await user_can_view(self.scope["user"], pk)

    @ws_handler
    async def handle_reply(self, message: ReplyMessage) -> ReplyCreatedMessage:
        return ReplyCreatedMessage(payload=message.payload)

    @event_handler
    async def handle_new_reply(self, event: NewReplyEvent) -> ReplyCreatedMessage:
        return ReplyCreatedMessage(payload=event.payload)

And you use it like this:

class HubConsumer(AsyncJsonWebsocketConsumer):
    authenticator_class = JWTAuthenticator
    topics = [DiscussionTopic, RoomTopic]

In short, topics let you multiplex: subscribe, publish messages, unsubscribe, and so on, all over the same socket. So you can reuse a single WebSocket connection and just add or compose multiple topics, i.e. multiple WebSocket handlers.

That made me think: if we could create reusable topics such as Notification, Streaming, Voice, AI Agent, and so on, which users could easily install or copy and then modify or inherit from in a structured way, WebSocket handling would become much more structured and easier. The idea is similar to DRF and its ecosystem, and the composable/reusable part would work like shadcn: copy it, own it, and modify the code freely.

What would you use it for? As I mentioned above: notifications, streaming, voice, AI agents, and so on. I have done a lot of WebSocket work, and I keep having to redefine the same things over and over. There is no reusable approach like the ones we have for REST APIs. Another example is using Pydantic AI with the AG-UI protocol but over WebSockets, defined in a reusable way.

So, if you already know of an existing open source solution or library similar to this idea, it would be great if you could share it here. And if this resonates with you, a comment would help, both to add more insight and to give some encouragement to actually build this.

reddit.com
u/huygl99 — 6 days ago
▲ 2 r/FastAPI+1 crossposts

How are you actually handling API abuse in FastAPI? Scrapers, credential stuffing, bots...

There's a lot of noise about this and almost no data. FastAPI ships no security layer, so everyone solves it somewhere: in the app, at the proxy, at the CDN, or not at all.

- What's actually in front of your API right now? nginx, Cloudflare, an API gateway, a rate limit library, middleware you wrote, nothing,...?
- Did you add it before or after something happened?
- What does it not do that you wish it did?

I'll collect whatever comes back here and publish the results.

If you'd rather answer privately: https://guard-core.com/survey

Thanks!

u/PA100T0 — 9 days ago
▲ 22 r/FastAPI+4 crossposts

A Ready-to-Use, Self-Hostable Backend for AI Chatbots

I open-sourced the backend we built to make AI chatbots actually do things 🤖

Hey everyone! 👋

The problem we kept running into with AI chatbots was simple:

A chatbot can generate a great response, but how does it actually take action?

Getting an AI to reliably run code, call APIs, use tools, work with files, and execute multi-step tasks requires a lot of backend infrastructure — building all of this ourselves was difficult and time-consuming.

So we built AI Skill Engine and have now open-sourced it.

GitHub: AI Skill Engine

You add AI Skills through the built-in admin dashboard, connect your chatbot to the Skill Engine API, and once connected, it behaves more like Claude or OpenAI models with tool-use capabilities.

It's fully self-hostable and can be connected to your chatbot through a Chat Completion API.

We originally built this for our internal use and decided to open-source it.

⭐ If you're building AI agents, copilots, or tool-using chatbots, I'd love for you to check it out and share your feedback.

u/sandeshnaroju — 13 days ago

My Telegram bot is running on GitHub + Render, but I'm not happy with the setup. What would you recommend?

​

Hey everyone,

I'm building a Python Telegram bot using aiogram. It's basically a video bot where I'm the only person who can upload videos, and users can access videos through Telegram links.

My current setup is:

- Python

- aiogram

- GitHub

- Render

- SQLite

- Telegram Bot API

The bot is working and polling Telegram successfully, but I'm having some problems with the overall setup and I'm not sure if GitHub + Render is the right choice for this project.

The features I need are pretty simple:

- Bot should run 24/7

- Only the owner/admin can upload videos

- Videos should remain available after restarts/redeployments

- I want a Get Link option where I select an uploaded video and the bot generates a Telegram deep link

- Someone opening that link should receive/play that specific video through the bot

- Normal users should only be able to watch videos and shouldn't be able to upload anything

- I want the setup to be reliable and reasonably cheap

Right now I'm using Render for hosting, but I'm not really happy with the experience and I'm wondering if there is a better approach.

What would you recommend?

Should I:

  1. Stay with Render and change something about my setup?

  2. Move to a VPS?

  3. Use another platform for Python/Telegram bots?

  4. Use PostgreSQL instead of SQLite?

  5. Use some kind of persistent disk/storage?

I'm still learning deployment, so I'd really appreciate advice from people who have hosted Telegram bots in production.

I'm not necessarily looking for the cheapest option — I mainly want something stable, simple, and capable of running 24/7 without constantly worrying about the service.

Thanks!

reddit.com
u/Realistic_Deer4555 — 12 days ago