-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsocket.html
More file actions
407 lines (352 loc) · 16 KB
/
socket.html
File metadata and controls
407 lines (352 loc) · 16 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="./img/favicon.png" type="image/x-icon" />
<link href="https://cdn.jsdelivr.net/npm/prismjs@1.25.0/themes/prism-tomorrow.min.css" rel="stylesheet" />
<link rel="stylesheet" href="./css/style.css">
<link rel="stylesheet" href="./css/socket.css">
<script src="./js/expandable.js" defer></script>
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.25.0/prism.min.js" defer></script>
<script
src="https://cdn.jsdelivr.net/npm/prismjs@1.30.0/plugins/normalize-whitespace/prism-normalize-whitespace.min.js"
defer></script>
<title>StreamElements Socket.io</title>
</head>
<body data-theme="dark">
<div class="header">
<div class="header-content">
<!-- <img src="logo.png" alt="Logo" class="logo" /> -->
<a href="index.html">
<p class="title">StreamElements API</p>
</a>
<a href="socket.html">
<p class="subtitle">StreamElements socket.io</p>
</a>
<a href="eventreceived.html">
<p class="subtitle">onEventReceived examples</p>
</a>
</div>
</div>
<h1>StreamElements socket.io</h1>
<div class="main">
<h2>What is it?</h2>
<p>
StreamElements socket.io (also known as realtime) is a socket implementation that allows you to connect to
StreamElements to send and receive events in real time.
</p>
<p>
As the name suggests, it is based on the <a href="https://socket.io/" target="_blank">socket.io</a>
protocol. While somewhat outdated on the StreamElements side (in favor of Astro WebSockets), it is still in
use and works well.
</p>
<br />
<p>Unlike Astro (<a href="https://docs.streamelements.com/websockets" target="_blank">StreamElements
WebSockets</a>), realtime allows you to send events to StreamElements. So, if you have a highly customized
widget, you can send events to StreamElements and have it respond accordingly.
</p>
<br />
<p>
For example, I have a custom widget that displays the Pokémon in my party from the Pokémon Yellow game (yeah, I
like old games). Every time a Pokémon's status changes (like HP), I can send an event to update the widget (check
Pikachu's HP in the gif).
</p>
<br />
<div class="gif-container" id="gif-container">
<img src="img/pokemon.gif" alt="pokemon gif" width="400" />
</div>
<h2>Initial information:</h2>
<p>
The StreamElements socket.io URL is <strong>https://realtime.streamelements.com</strong>.
<br />
</p>
<p>
You can authenticate using <span class="code">bearer</span> token (JWT), <span class="code">apikey</span> (overlay
token), or <span class="code">oAuth2</span> token. It uses "websocket" transport.
</p>
<br />
<p>
It also appears possible to connect directly via Websockets (not socket.io) to
<strong>wss://realtime.streamelements.com</strong>, but this document only covers the socket.io implementation
(although most details apply to both).
</p>
<br />
<p>Official StreamElements socket.io documentation is on <a
href="https://dev.streamelements.com/docs/api-docs/5a84cc101a9c5-connecting-via-websocket-using-o-auth2"
target="_blank"> Streamelements Developer Portal </a>
</p>
<h2>
How to connect:
</h2>
<p>
It seems StreamElements still uses an old version of Socket.io, version 2. I was able to connect using newer
client version, like 4.5.4, though. However, if you are having connection issues, try using an older client
version to see if that works.
</p>
<p>
You need to use a socket.io library to connect to the StreamElements socket. It is originally a JavaScript
library, but you can find other client implementations on the <a
href="https://socket.io/docs#client-implementations" target="_blank">Socket.io website</a>
</p>
<section class="expandable">
<button class="expand-toggle" aria-expanded="false">
<span class="icon">▶️</span>
<span class="label">Click to expand</span>
</button>
<div class="expand-content" hidden>
<div class="code-container">
<div class="code-header">
<span>HTML</span>
<button data-clipboard>Copy</button>
</div>
<pre><code class="language-html">
<script src="https://cdn.socket.io/4.5.4/socket.io.min.js"></script>
</code></pre>
</div>
<div class="code-container">
<div class="code-header">
<span>Javascript</span>
<button data-clipboard>Copy</button>
</div>
<pre><code class="language-javascript">
// This snippet is just an example of how to connect to the StreamElements socket.
// For more information on handling events, check the next section
const url = "https://realtime.streamelements.com";
const options = { transports: ["websocket"]};
const token = ""; // Your JWT, overlay token (aka apikey) or oAuth2 token
const method = ""; // "jwt", "apikey" or "oauth2", based on the token used
const socket = io(url, options); // Start the connection
// Listen for connection and authentication events
socket.on("connect", onConnect);
socket.on("authenticated", onAuthenticated);
socket.on("disconnect", onDisconnect);
socket.on("unauthorized", console.error);
function onConnect() {
console.log("Connected to StreamElements socket.");
// After connecting, authenticate:
socket.emit("authenticate", { method: method, token: token });
}
function onAuthenticated(data) {
const { channelId, clientId, message, project } = data; // available fields in the response
console.log(`Successfully authenticated to channel ${channelId} with ID ${clientId}`);
console.log(message);
}
function onDisconnect(data) {
// The client will try to reconnect automatically unless the reason is
// "io server disconnect" and "io client disconnect" (manual disconnections)
// If you want to reconnect even when there is a manual disconnects, use:
// if (reason === "io server disconnect" || reason === "io client disconnect") {
// socket.connect();
// }
console.log(`Disconnected from StreamElements. Reason: ${data}`);
}
</code></pre>
</div>
</div>
</section>
<h2>
Events
</h2>
<p>Every time a new event occurs, the socket emits a message (except for a chat messages). Some events
are listed below:</p>
<!-- More events might be listed at: https://gist.github.com/styler/c6869f303be2e3f0dd05c68a3479bfeb -->
<div class="code-container">
<div class="code-header">
<span>Reference</span>
<button data-clipboard>Copy</button>
</div>
<pre><code class="language-javascript">
"event" // Standard events like follower, subscribers, channel points redemptions
"event:test" // Test events and When the "Next Alert" button is clicked (useful for custom events)
"event:update" // Same as "event", but updates data
"event:skip" // When alert is skipped
"session:reset" // When session data is reset
"overlay:mute" // When overlays are muted or unmuted
"overlay:refresh" // When overlays are refreshed
"overlay:update" // When an overlay is updated
"overlay:action" // When the "Reload overlays" is clicked on Activity feed
"overlay:togglequeue" // When overlays are paused or resumed
"kvstore:update" // When a kvstore data is updated (like overlay paused)
"session:update" // When an session update occurs (new bits, new follower, tips, etc)
"bot:counter" // When a counter is updated
"giveaway:state" // When a giveaway is created or closed
"contest:state" // When a contest is created or closed
"contest:winner" // When a contest winner is picked
"songrequest::mediashare" // Information on media share
"songrequest:play" // When song request starts playing
"songrequest:pause" // When song request is paused
"songrequest:song:next" // When next song is chosen to be played
"songrequest:song:previous" // When previous song is chosen to be played
"songrequest:queue:remove" // When song is removed from queue (or declined from pending requests)
"songrequest:queue:add" // When song is added to the queue (or approved from pending requests)
"songrequest:history:add" // When song is added to the history
"songrequest:volume" // When volume is set for song
"songrequest:settings:update" // When song request settings is changed
</code></pre>
</div>
<h3>Receiving events</h3>
<p>In order to receive events, add a <span class="code">socket.on</span> function with the event name you want to
receive:</p>
<p>If you want, you can listen to all the events listed above, just register each one in its own code block.</p>
<section class="expandable">
<button class="expand-toggle" aria-expanded="false">
<span class="icon">▶️</span>
<span class="label">Click to expand</span>
</button>
<div class="expand-content" hidden>
<div class="code-container">
<div class="code-header">
<span>Javascript</span>
<button data-clipboard>Copy</button>
</div>
<pre><code class="language-javascript">
socket.on("event", (data) => {
console.log("event received:", data);
// Your logic here
});
socket.on("event:test", (data) => {
console.log("event:test received:", data);
// Your logic here
});
socket.on("overlay:action", (data) => {
console.log("overlay:action received:", data);
// Your logic here
});
</code></pre>
</div>
</div>
</section>
<p>
Alternatively, you can use <span class="code">socket.onAny()</span> function to listen to all events:
</p>
<section class="expandable">
<button class="expand-toggle" aria-expanded="false">
<span class="icon">▶️</span>
<span class="label">Click to expand</span>
</button>
<div class="expand-content" hidden>
<div class="code-container">
<div class="code-header">
<span>Javascript</span>
<button data-clipboard>Copy</button>
</div>
<pre><code class="language-javascript">
socket.onAny((eventName, ...args) => {
console.log("New event:", eventName);
console.log("Data:", args[0]);
// Your logic here
});
</code></pre>
</div>
</div>
</section>
<h2>
Rooms
</h2>
<p>
Some rooms are available to subscribe to via the StreamElements socket. Here are the ones I have found so far
(replace <span class="code">ACCOUNT_ID</span> with your actual StreamElements account id):
</p>
<div class="code-container">
<div class="code-header">
<span>Reference</span>
<button data-clipboard>Copy</button>
</div>
<pre><code class="language-javascript">
"songrequest::ACCOUNT_ID"
"contests::ACCOUNT_ID"
"giveaways::ACCOUNT_ID"
"store::ACCOUNT_ID"
"obs::ACCOUNT_ID"
"kvstore::ACCOUNT_ID"
"plarium::ACCOUNT_ID"
"appsflyer::ACCOUNT_ID"
"partner-integrations::ACCOUNT_ID"
"audience-queue::ACCOUNT_ID"
</code></pre>
</div>
<p>
Before you ask, I also don't know exactly what the "plarium" and "appsflyer" rooms are used for. Plarium is the
company behind the game “Raid - Shadow Legends” (a well-known StreamElements sponsorship) and AppsFlyer is a
company that does analytics and campaigns management. The "partner-integrations" room is probably related to
sponsorships, but I could not test it.
</p>
<p>
As for "obs", well... I believe it has something to do with OBS Studio, but I couldn't test it. If you know more
about it, please let me know.
</p>
<h3>
Subscribe to a room
</h3>
<p>
To join a room, emit a <span class="code">subscribe</span> event with the room name. In JavaScript, you also need
to provide a third parameter: a callback function. So, it should look like this:
</p>
<section class="expandable">
<button class="expand-toggle" aria-expanded="false">
<span class="icon">▶️</span>
<span class="label">Click to expand</span>
</button>
<div class="expand-content" hidden>
<div class="code-container">
<div class="code-header">
<span>Javascript</span>
<button data-clipboard>Copy</button>
</div>
<pre><code class="language-javascript">
const id = "5a2ed5cc9a474a2c2bc4f0aa"; // your account id
socket.emit("subscribe" , {"room": `giveaways::${id}`} , (error, data) => console.log(error, data));
</code></pre>
</div>
</div>
</section>
<p>
Alternatively, you can subscribe to multiple rooms at once:
</p>
<section class="expandable">
<button class="expand-toggle" aria-expanded="false">
<span class="icon">▶️</span>
<span class="label">Click to expand</span>
</button>
<div class="expand-content" hidden>
<div class="code-container">
<div class="code-header">
<span>Javascript</span>
<button data-clipboard>Copy</button>
</div>
<pre><code class="language-javascript">
const id = "5a2ed5cc9a474a2c2bc4f0aa"; // your account id
const rooms = [
"songrequest", "contests", "giveaways", "kvstore", "store",
"obs", "audience-queue", "partner-integrations", "plarium", "appsflyer"
];
rooms.forEach( (room) => {
socket.emit("subscribe" , {"room": `${room}::${id}`} , (error, data) => {
if(error){
console.error(`Error subscribing to ${room}::${id}:\n ${error.message}`);
return;
}
console.log(data);
});
});
</code></pre>
</div>
</div>
</section>
<h2>Other info</h2>
<p>
If you have questions or want to share feedback, feel free to <a href="https://c4ldas.com.br/contact"
target="_blank" rel="noopener noreferrer">contact me here</a>.
</p>
<p>
I believe there is much more data to document, but I can't recall what else to add right now. In the meantime,
enjoy this randomly generated image below:
</p>
<br />
<img src="https://picsum.photos/1280/720">
</div>
</div>
<br /><br /><br />
</body>
</html>