Skip to content

Commit 04bd250

Browse files
committed
Adds long-running websocket stream consumer example
1 parent 606f91f commit 04bd250

File tree

5 files changed

+238
-0
lines changed

5 files changed

+238
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# WebSocket Stream Consumer - Bluesky Firehose
2+
3+
This example demonstrates a long-running Durable Object that connects to the Bluesky firehose (via Jetstream) and filters for post events, with rate limiting to print at most 1 per second.
4+
5+
## How to Run
6+
7+
First ensure that `uv` is installed:
8+
https://docs.astral.sh/uv/getting-started/installation/#standalone-installer
9+
10+
Now, if you run `uv run pywrangler dev` within this directory, it should use the config
11+
in `wrangler.jsonc` to run the example.
12+
13+
You can also run `uv run pywrangler deploy` to deploy the example.
14+
15+
## Testing the Firehose Consumer
16+
17+
1. Start the worker: `uv run pywrangler dev`
18+
2. Make any request to initialize the DO: `curl "http://localhost:8787/status"`
19+
3. Watch the logs to see filtered Bluesky post events in real-time (rate limited to 1/sec)!
20+
21+
The Durable Object automatically connects to Jetstream when first accessed. It will maintain a persistent WebSocket connection and print out post events to the console, including the author DID, post text (truncated to 100 chars), and timestamp. Posts are rate limited to display at most 1 per second to avoid overwhelming the logs.
22+
23+
**Available endpoints:**
24+
- `/status` - Check connection status
25+
- `/reconnect` - Manually trigger reconnection if disconnected
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "python-websocket-stream-consumer",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"deploy": "uv run pywrangler deploy",
7+
"dev": "uv run pywrangler dev",
8+
"start": "uv run pywrangler dev"
9+
},
10+
"devDependencies": {
11+
"wrangler": "^4.46.0"
12+
}
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[project]
2+
name = "python-websocket-stream-consumer"
3+
version = "0.1.0"
4+
description = "Python WebSocket stream consumer example"
5+
readme = "README.md"
6+
requires-python = ">=3.12"
7+
dependencies = [
8+
"webtypy>=0.1.7",
9+
]
10+
11+
[dependency-groups]
12+
dev = [
13+
"workers-py",
14+
"workers-runtime-sdk"
15+
]
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
from workers import WorkerEntrypoint, Response, DurableObject
2+
import js
3+
import json
4+
import time
5+
from pyodide.ffi import create_proxy
6+
from urllib.parse import urlparse
7+
8+
9+
class BlueskyFirehoseConsumer(DurableObject):
10+
"""Durable Object that maintains a persistent WebSocket connection to Bluesky Jetstream."""
11+
12+
def __init__(self, state, env):
13+
super().__init__(state, env)
14+
self.websocket = None
15+
self.connected = False
16+
self.last_print_time = 0 # Track last time we printed a post
17+
18+
async def fetch(self, request):
19+
"""Handle incoming requests to the Durable Object."""
20+
# If we're not connected then make sure we start a connection.
21+
if not self.connected:
22+
await self._schedule_next_alarm()
23+
await self._connect_to_jetstream()
24+
25+
url = urlparse(request.url)
26+
path = url.path
27+
28+
if path == "/status":
29+
status = "connected" if self.connected else "disconnected"
30+
return Response(f"Firehose status: {status}")
31+
else:
32+
return Response("Available endpoints: /status")
33+
34+
async def alarm(self):
35+
"""Handle alarm events - used to ensure that the DO stays alive and connected"""
36+
print("Alarm triggered - making sure we are connected to jetstream...")
37+
if not self.connected:
38+
await self._connect_to_jetstream()
39+
else:
40+
print("Already connected, skipping reconnection")
41+
42+
# Schedule the next alarm to keep the DO alive
43+
await self._schedule_next_alarm()
44+
45+
async def _schedule_next_alarm(self):
46+
"""Schedule the next alarm to run in 1 minute to keep the DO alive."""
47+
# Schedule alarm for 1 minute from now, overwriting any existing alarms
48+
next_alarm_time = int(time.time() * 1000) + 60000
49+
return await self.ctx.storage.setAlarm(next_alarm_time)
50+
51+
async def _on_open(self, event):
52+
"""Handle WebSocket open event."""
53+
self.connected = True
54+
print("Connected to Bluesky Jetstream firehose!")
55+
print("Filtering for: app.bsky.feed.post (post events, rate limited to 1/sec)")
56+
# Ensure alarm is set when we connect
57+
await self._schedule_next_alarm()
58+
59+
def _on_message(self, event):
60+
"""Handle incoming WebSocket messages."""
61+
try:
62+
# Parse the JSON message
63+
data = json.loads(event.data)
64+
65+
# Store the timestamp for resumption on reconnect
66+
time_us = data.get("time_us")
67+
if time_us:
68+
# Store the timestamp asynchronously
69+
self.ctx.storage.kv.put("last_event_timestamp", time_us)
70+
71+
# Jetstream sends different event types
72+
# We're interested in 'commit' events which contain posts
73+
if data.get("kind") == "commit":
74+
commit = data.get("commit", {})
75+
collection = commit.get("collection")
76+
77+
# Filter for post events
78+
if collection == "app.bsky.feed.post":
79+
# Rate limiting: only print at most 1 per second
80+
current_time = time.time()
81+
if current_time - self.last_print_time >= 1.0:
82+
record = commit.get("record", {})
83+
print("Post record", record)
84+
85+
# Update last print time
86+
self.last_print_time = current_time
87+
88+
except Exception as e:
89+
print(f"Error processing message: {e}")
90+
91+
def _on_error(self, event):
92+
"""Handle WebSocket error event."""
93+
print(f"WebSocket error: {event}")
94+
self.connected = False
95+
self.ctx.abort("WebSocket error occurred")
96+
97+
async def _on_close(self, event):
98+
"""Handle WebSocket close event."""
99+
print(f"WebSocket closed: code={event.code}, reason={event.reason}")
100+
self.connected = False
101+
self.ctx.abort("WebSocket closed unexpectedly")
102+
103+
async def _connect_to_jetstream(self):
104+
"""Connect to the Bluesky Jetstream WebSocket and start consuming events."""
105+
# Get the last event timestamp from storage to resume from the right position
106+
last_timestamp = self.ctx.storage.kv.get("last_event_timestamp")
107+
108+
# Jetstream endpoint - we'll filter for posts
109+
# Using wantedCollections parameter to only get post events
110+
jetstream_url = "wss://jetstream2.us-east.bsky.network/subscribe?wantedCollections=app.bsky.feed.post"
111+
112+
# If we have a last timestamp, add it to resume from that point
113+
if last_timestamp:
114+
jetstream_url += f"&cursor={last_timestamp}"
115+
print(
116+
f"Connecting to Bluesky Jetstream at {jetstream_url} (resuming from timestamp: {last_timestamp})"
117+
)
118+
else:
119+
print(
120+
f"Connecting to Bluesky Jetstream at {jetstream_url} (starting fresh)"
121+
)
122+
123+
# Create WebSocket using JS FFI
124+
ws = js.WebSocket.new(jetstream_url)
125+
self.websocket = ws
126+
127+
# Attach event handlers
128+
#
129+
# Note that ordinarily proxies need to be destroyed once they are no longer used.
130+
# However, in this Durable Object context, the WebSocket and its event listeners
131+
# persist for the lifetime of the Durable Object, so we don't explicitly destroy
132+
# the proxies here. When the websocket connection closes, the Durable Object
133+
# is restarted which destroys these proxies.
134+
#
135+
# In the future, we plan to provide support for native Python websocket APIs which
136+
# should eliminate the need for proxy wrappers.
137+
ws.addEventListener("open", create_proxy(self._on_open))
138+
ws.addEventListener("message", create_proxy(self._on_message))
139+
ws.addEventListener("error", create_proxy(self._on_error))
140+
ws.addEventListener("close", create_proxy(self._on_close))
141+
142+
143+
class Default(WorkerEntrypoint):
144+
"""Main worker entry point that routes requests to the Durable Object."""
145+
146+
async def fetch(self, request):
147+
# Get the Durable Object namespace from the environment
148+
namespace = self.env.BLUESKY_FIREHOSE
149+
150+
# Use a fixed ID so we always connect to the same Durable Object instance
151+
# This ensures we maintain a single persistent connection
152+
id = namespace.idFromName("bluesky-consumer")
153+
stub = namespace.get(id)
154+
155+
# Forward the request to the Durable Object
156+
return await stub.fetch(request)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"$schema": "node_modules/wrangler/config-schema.json",
3+
"name": "python-websocket-stream-consumer",
4+
"main": "src/entry.py",
5+
"compatibility_date": "2025-11-02",
6+
"compatibility_flags": [
7+
"python_workers"
8+
],
9+
"observability": {
10+
"enabled": true
11+
},
12+
"durable_objects": {
13+
"bindings": [
14+
{
15+
"name": "BLUESKY_FIREHOSE",
16+
"class_name": "BlueskyFirehoseConsumer",
17+
"script_name": "python-websocket-stream-consumer"
18+
}
19+
]
20+
},
21+
"migrations": [
22+
{
23+
"tag": "v1",
24+
"new_sqlite_classes": [
25+
"BlueskyFirehoseConsumer"
26+
]
27+
}
28+
]
29+
}

0 commit comments

Comments
 (0)