Skip to content

Commit e4fd121

Browse files
committed
Add example usage of broadcaster
1 parent ec2b98d commit e4fd121

File tree

6 files changed

+82
-3
lines changed

6 files changed

+82
-3
lines changed

django-subscriptions/api/asgi.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1+
import typing
2+
3+
from starlette.requests import Request
4+
from starlette.websockets import WebSocket
15
from strawberry.asgi import GraphQL
26

7+
from .context import Context, get_broadcast
8+
39

410
class MyGraphQL(GraphQL):
5-
...
11+
async def get_context(
12+
self, request: typing.Union[Request, WebSocket]
13+
) -> typing.Optional[typing.Any]:
14+
broadcast = await get_broadcast()
15+
16+
return Context(broadcast)

django-subscriptions/api/context.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from dataclasses import dataclass
2+
3+
from broadcaster import Broadcast
4+
5+
broadcast = None
6+
7+
8+
async def get_broadcast():
9+
global broadcast
10+
11+
if not broadcast:
12+
broadcast = Broadcast("memory://")
13+
14+
await broadcast.connect()
15+
16+
return broadcast
17+
18+
19+
@dataclass
20+
class Context:
21+
broadcast: Broadcast

django-subscriptions/api/schema.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,35 @@ def hello() -> str:
1010
return "world"
1111

1212

13+
@strawberry.type
14+
class Mutation:
15+
@strawberry.field
16+
async def send_message(self, info, message: str) -> bool:
17+
print("sending on_message")
18+
print(id(info.context.broadcast))
19+
await info.context.broadcast.publish(channel="chatroom", message=message)
20+
21+
return True
22+
23+
1324
@strawberry.type
1425
class Subscription:
26+
@strawberry.subscription
27+
async def on_message(self, info) -> str:
28+
print("starting on_message")
29+
print(id(info.context.broadcast))
30+
31+
async with info.context.broadcast.subscribe(channel="chatroom") as subscriber:
32+
print(f"{subscriber=}")
33+
async for event in subscriber:
34+
print(f"{event=}")
35+
yield event.message
36+
1537
@strawberry.subscription
1638
async def count(self, target: int = 100) -> int:
1739
for i in range(target):
1840
yield i
1941
await asyncio.sleep(0.5)
2042

2143

22-
schema = strawberry.Schema(query=Query, subscription=Subscription)
44+
schema = strawberry.Schema(query=Query, mutation=Mutation, subscription=Subscription)

django-subscriptions/api/views.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@
88
from django.template.response import TemplateResponse
99
from strawberry.django.views import AsyncGraphQLView as StrawberryAsyncGraphQLView
1010

11+
from .context import Context, get_broadcast
12+
1113

1214
class AsyncGraphQLView(StrawberryAsyncGraphQLView):
15+
async def get_context(self, request: HttpRequest) -> Context:
16+
broadcast = await get_broadcast()
17+
18+
return Context(broadcast)
19+
1320
def _render_graphiql(self, request: HttpRequest, context=None):
1421
if not self.graphiql:
1522
raise Http404()

django-subscriptions/poetry.lock

Lines changed: 18 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

django-subscriptions/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ python = "^3.9"
1010
strawberry-graphql = "^0.46.0"
1111
Django = "^3.1.7"
1212
uvicorn = {extras = ["standard"], version = "^0.13.4"}
13+
broadcaster = "^0.2.0"
1314

1415
[tool.poetry.dev-dependencies]
1516
black = {version = "^20.8b1", allow-prereleases = true}

0 commit comments

Comments
 (0)