Skip to content

Connecting Node.JS To StreamElements WebSocket

If you're planning on using the Streamelements Websockets service to receive realtime notifications of things like tips, merch purchases and stream store redemptions (it can also do follows, cheers, raids, hosts and subs for Twitch), you may find information is a bit thin on the ground, so here's a quick guide to getting connected using TypeScript and Node.js.


I'm reviewing this post as I'm migrating it into my new website and it's worth pointing out that as with many of these providers it seems the API is quite fluid. So this code may not work anymore. It's also worth pointing out that you should check the latest StreamElements documentation for the latest API information (although to be fair, I'm not hopeful it will be any good).

First things first, the library required to connect to the realtime service. The examples provided by Streamelements are based on the library, but what may not be obvious with a cursory glance is the version requirement (I skipped the HTML example and went straight to npm to install the client). The library server and client have to operate on the same version and Streamelements use version 2 on the server, so you have to run the version 2 client. Library Installation

npm install --save

With the correct client installed, you're good to go, so lets look at some sample code. NOTE:- This code is a butchered version of the class within my bot that links to Streamelements, as such there may be minor issues, but it should give you the general idea.

Sample Code

Sample TypeScript Code
import io, { Socket } from "";

var jwttoken: string = '<YOUR JWT TOKEN>';
var socket: Socket;
var channelid: string = '';
var clientid: string = '';
const passthroughtestevents: boolean = false;

socket = io('', {transports: ['websocket']});

socket.on('connect', () => { socketConnect(); });
socket.on('connect_error', (data) => { socketConnectError(data); });
socket.on('disconnect', () => { socketDisconnect(); });
socket.on('authenticated', (data) => { socketAuthenticated(data); });
socket.on('unauthorized', (data) => { socketUnauthorized(data); });
socket.on('event:test', (data) => { socketEventTest(data); });
socket.on('event', (data) => { socketEvent(data); });
socket.on('event:update', (data) => { socketEventUpdate(data); });
socket.on('event:reset', (data) => { socketEventReset(data); });

function socketConnect() {
    console.log(`Connected, authenticating`);
    socket.emit('authenticate', { method: 'jwt', token: jwttoken });

function socketDisconnect() {
    // You don't need to worry about reconnecting, unless you have turned off automatic reconnect (enabled by default)

function socketAuthenticated(data) {
    /* Sample data
    {"clientId":"<YOUR CLIENT ID>","channelId":"<YOUR CHANNEL ID>","project":"realtime","message":"You are in a maze of dank memes, all alike."}

    channelid = data.channelId;
    clientid = data.clientId;

    console.log(`Connected to ${channelid}`);

function socketUnauthorized(data) {
    console.log(`Authentication failed - ${JSON.stringify(data)}`);

function socketEventTest(data) {
    // Events raised with the 'Emulate' feature of the overlay editor will arrive as this handler
    // During development, you may with to pass them through to the main handler to test out the rest of your code

    if (passthroughtestevents) {

function socketEvent(data) {
    var listener: string = data.listener;
    var event = data.event;

    switch (listener) {
        case 'redemption-latest':
            console.log(`Stream store redemption - ${JSON.stringify(event)}`);
        case 'tip-latest':
            console.log(`Tip - ${JSON.stringify(event)}`);
        case 'merch-latest':
            console.log(`Merch store purchase - ${JSON.stringify(event)}`);

function socketEventUpdate(data) {
    console.log(`Session data update event - ${JSON.stringify(data)}`);

function socketEventReset(data) {
    console.log(`Session data reset event - ${JSON.stringify(data)}`);

This sample code, is only looking for the latest redemptions, tips and merch purchases.