-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathbluesky-firehose.html
175 lines (157 loc) · 5.23 KB
/
bluesky-firehose.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bluesky WebSocket Feed Monitor</title>
<style>
body {
font-family: system-ui, -apple-system, sans-serif;
max-width: 800px;
margin: 20px auto;
padding: 0 20px;
}
.textarea-container {
margin: 20px 0;
}
textarea {
width: 100%;
height: 200px;
margin-top: 10px;
padding: 10px;
font-family: monospace;
border: 1px solid #ccc;
border-radius: 4px;
}
#output {
height: 400px;
}
.controls {
margin: 20px 0;
}
button {
padding: 8px 16px;
margin-right: 10px;
cursor: pointer;
}
.error {
color: red;
margin-top: 5px;
font-size: 14px;
}
</style>
</head>
<body>
<h1>Bluesky WebSocket Feed Monitor</h1>
<div class="controls">
<button id="connect">Connect</button>
<button id="disconnect">Disconnect</button>
<button id="clear">Clear Log</button>
</div>
<div class="textarea-container">
<h3>Send Message</h3>
<textarea id="input" placeholder="Paste your JSON message here..."></textarea>
<div id="jsonError" class="error"></div>
<button id="send">Send Message</button>
<p>Example message to filter for only specific DID handles (<a href="https://tools.simonwillison.net/bluesky-resolve">resolve those here</a>):</p>
<pre><code>
{
"type": "options_update",
"payload": {
"wantedCollections": ["app.bsky.feed.post"],
"wantedDids": ["did:plc:kft6lu4trxowqmter2b6vg6z"],
"maxMessageSizeBytes": 1000000
}
}</code></pre>
</div>
<div class="textarea-container">
<h3>Log Output</h3>
<textarea id="output" readonly></textarea>
</div>
<script>
let ws = null;
const output = document.getElementById('output');
const input = document.getElementById('input');
const jsonError = document.getElementById('jsonError');
function log(message) {
const timestamp = new Date().toISOString();
output.value += `[${timestamp}] ${message}\n`;
output.scrollTop = output.scrollHeight;
}
function connect() {
if (ws) {
log('Already connected!');
return;
}
try {
ws = new WebSocket('wss://jetstream2.us-east.bsky.network/subscribe?wantedCollections=app.bsky.feed.post');
ws.onopen = () => {
log('Connected to Bluesky WebSocket');
};
ws.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
log(`Received: ${JSON.stringify(data, null, 2)}`);
} catch (e) {
log(`Raw message: ${event.data}`);
}
};
ws.onerror = (error) => {
log(`WebSocket Error: ${error.message}`);
};
ws.onclose = () => {
log('Disconnected from Bluesky WebSocket');
ws = null;
};
} catch (error) {
log(`Connection Error: ${error.message}`);
ws = null;
}
}
function disconnect() {
if (ws) {
ws.close();
ws = null;
log('Disconnected by user');
} else {
log('Not connected');
}
}
function sendMessage() {
if (!ws) {
log('Not connected to WebSocket');
return;
}
const message = input.value.trim();
if (!message) {
jsonError.textContent = 'Please enter a message';
return;
}
try {
// Validate JSON
const jsonMessage = JSON.parse(message);
// Send the message
ws.send(JSON.stringify(jsonMessage));
log(`Sent: ${message}`);
jsonError.textContent = '';
} catch (error) {
jsonError.textContent = 'Invalid JSON format';
log(`Failed to send message: Invalid JSON format`);
}
}
// Event Listeners
document.getElementById('connect').addEventListener('click', connect);
document.getElementById('disconnect').addEventListener('click', disconnect);
document.getElementById('clear').addEventListener('click', () => {
output.value = '';
});
document.getElementById('send').addEventListener('click', sendMessage);
// Add keyboard shortcut (Ctrl/Cmd + Enter) to send message
input.addEventListener('keydown', (e) => {
if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') {
sendMessage();
}
});
</script>
</body>
</html>