Introduction

Constellation is our daemon responsible for stateful aspects of Beam. One of it's features is Live Loading. Live Loading let's you recieve realtime updates of models and resources as they change on beam.

In this tutorial we're going to connect to Constellation and subscribe to Live Loading updates of your channel. If your viewer number or channel title change you'll recieve an update within your code that you can respond to.

Writing the Code

Select a language below.

Prerequisites

  1. Get NodeJS and NPM for your platform.
  2. Create a new project with npm.
  3. Run npm install --save carina ws

Usage

Carina is Beam's Node Constellation client. It makes talking to Constellation super easy.

Let's start by importing and setting up all of the modules which we'll need and defining our channel id.

You can find your channel id by going to https://beam.pro/api/v1/channels/username?fields=id in your browser. Replacing username with your Beam username.

const Carina = require('carina').Carina;
const ws = require('ws');

Carina.WebSocket = ws;

const channelId = 1234;

Next we'll create an instance of Carina to use for our code. We pass an object of options to the constructor. isBot must be set to true if you're writing an automated bot.

const ca = new Carina({ isBot: true }).open();

To recieve Live Loading events you need to subscribe to them. We'll just need channel:{id}:update. For a full list of events check our Constellation reference guide. You subscribe to events within Carina by using the subscribe method.

ca.subscribe(`channel:${channelId}:update`, data => {
    // ...
});

The second argument to the subscribe method is a function which will be called with the data when the event is sent. Inside the function you can do a lot with the data but for now we'll just log it to the console.

ca.subscribe(`channel:${channelId}:update`, data => {
    console.log(data);
});

That's it! Save the file as 'constellation.js' and run it with node constellation.js from your terminal. Try updating your channel details.

For example if you update your channel title from the Player to be 'test'. You'll get an object from the event that looks like this:

{ name: 'test' }

Try updating your channel description, age rating or selected game for more examples.

The final code can be found below:

const Carina = require('carina').Carina;
const ws = require('ws');

Carina.WebSocket = ws;

const channelId = 1234;

const ca = new Carina({ isBot: true }).open();

ca.subscribe(`channel:${channelId}:update`, data => {
    console.log(data);
});

Carina also works with TypeScript and your Browser. For more information on it checkout its Github page.

Further Ideas

  • Why not combine Constellation with our REST API and make something super cool?
  • Use some of the other Constellation events to get events about your stream.

Want More Info?

If you'd like more information on Constellation system, checkout the reference guide.

Need more help?

If you're still not sure, or would like some help, hit us up on Gitter!