encode/broadcaster

No notifications received with Postgres as backend

wkamer opened this issue · 5 comments

I'm trying to get broadcaster work with FastAPI and a PostgreSQL database with notifications enabled.

Here is the code I use

FastAPI (test_broadcaster.py)

from broadcaster import Broadcast

broadcast = Broadcast("postgres://<username>:<password>@localhost:5432/<database>")

async def broadcast_ws_receiver(websocket):
    async for message in websocket.iter_text():
        await broadcast.publish(channel="account", message=message)

async def broadcast_ws_sender(websocket):
    async with broadcast.subscribe(channel="account") as subscriber:
        async for event in subscriber:
            await websocket.send_text(event.message)

FastAPI (router.py)

from fastapi.routing import APIRouter
from fastapi.websockets import WebSocket
from fastapi.concurrency import run_until_first_complete

from test_broadcaster import broadcast_ws_sender, broadcast_ws_receiver

router = APIRouter()

@router.websocket("/broadcast")
async def broadcast_ws(websocket: WebSocket):
    await websocket.accept()
    
    await run_until_first_complete(
        (broadcast_ws_receiver, {"websocket": websocket}),
        (broadcast_ws_sender, {"websocket": websocket}),
    )

PostgreSQL (trigger name) # on the table account
account

PostgreSQL (trigger_function)

BEGIN
  PERFORM pg_notify(
    'account',
    json_build_object(
      'operation', TG_OP,
      'record', row_to_json(NEW)
    )::text
  );

  RETURN NEW;
END;

When making the websocket connection connection and update the table to trigger a notification, nothing happends. Also not when using

NOTIFY account, 'hello world';

When changing to Redis it is working flawless, from Redis to websocket and websocket to Redis.

Is this a bug? Or I'm missing something in the code?

Is this tool still under development? Hope to get some response.

@wkamer, it's worse than that, the maintainers don't even have the respect to put a message in the readme saying that it is no longer under development, in spite of lots of people submitting issues and PRs...

Hi folks - if you've a PR that you'd like to see merged that's currently blocked then let me know and I can add you to the maintainers team.

@wkamer - Don't know - I'm not currently using broadcaster myself, so depends if anyone in the community who's currently using broadcaster w/ postgres is able to chime in on the thread? (Or else whatever you're able to dig into yourself and fill in on, here.)

I'm using broadcaster with Redis backend and it works fine, but AFAIR that's only backend that does. Something is very wrong with our tests suite IMHO. It's supposed to test other backends like memory, Kafka and PostgreSQL and tests for those pass, but in real world suddenly they don't.