Guides: Get started with the Mux live stream API

0:00

Dave: Live streaming

0:00

used to be a pretty

0:01

complicated feature

0:02

to build into

0:03

your applications,

0:04

but not anymore.

0:05

With the use

0:06

of Mux's simple

0:07

APIs, you can

0:08

start building

0:09

live streaming

0:09

features into your

0:10

application in

0:11

just a few minutes.

0:12

In this video,

0:13

we're going to

0:13

talk through three

0:14

different steps that

0:15

you'll need to take

0:15

to build out live

0:16

streaming features

0:17

into your app.

0:18

The first is

0:19

create a unique

0:20

live stream.

0:21

Then we're going to

0:22

start streaming to

0:24

that live stream.

0:25

And then finally,

0:26

we'll figure out how

0:27

to play back that

0:28

live stream in your

0:29

own video player.

0:31

Let's get started

0:32

in the Mux

0:33

dashboard by Going

0:34

to create a brand

0:36

new access token

0:37

that we can use

0:37

with the MUX API.

0:39

I'm heading down to

0:40

the corner settings

0:41

and choosing

0:42

access tokens.

0:43

And I want to click

0:44

on the generate

0:45

new token button.

0:47

First, we'll

0:48

want to select

0:48

the environment

0:49

that we're

0:49

working within.

0:50

We only need

0:51

permissions to

0:52

work with MUX

0:52

video in this

0:53

particular use case.

0:54

You can change

0:55

the name of your

0:55

access token if it's

0:56

helpful and click

0:58

generate token.

1:00

So we'll need

1:00

both of these

1:01

values to create

1:02

the live stream.

1:03

Make sure to copy

1:04

them and paste them

1:05

somewhere safe.

1:06

You can also

1:06

click the green

1:07

button to download

1:08

the environment

1:08

variables file.

1:10

Cool.

1:10

So now we're ready

1:11

to start writing a

1:12

little bit of code.

1:13

I'm in a brand

1:13

new project

1:14

in my editor.

1:15

And the approach

1:16

that I'll take for

1:17

this particular demo

1:18

is to create a node

1:19

JS file that will

1:21

interact with the

1:22

mux API and create

1:23

the resources that

1:24

we need to start

1:25

live streaming.

1:26

So we'll have

1:27

one main entry

1:28

file, and I'll

1:29

call that index.

1:30

js, and this

1:32

file will perform

1:33

the actions that

1:34

we need to get

1:35

up and running.

1:36

To make working with

1:36

the Mux API super

1:38

easy, we can install

1:39

the MuxNode SDK

1:40

into our project.

1:42

You don't need to

1:43

be using Javascript

1:44

for your project.

1:45

You can be using

1:45

any language

1:46

that is right for

1:47

your application.

1:49

So now looking

1:49

at the project

1:50

structure, we

1:51

have a package.

1:52

json file, an index.

1:53

js, and our

1:54

associated packages.

1:56

That's all great,

1:57

but we really

1:58

only need a few

1:58

lines of code to

1:59

get started here.

2:01

So, the first

2:01

thing I want to

2:02

do is import the

2:03

muxnode SDK that

2:05

we just installed

2:06

to this project.

2:09

Perfect.

2:09

And now we'll need

2:10

to instantiate

2:11

it using those

2:11

environment

2:12

variables that

2:13

contain the access

2:14

token and the

2:15

secret key that

2:16

we created within

2:17

the MUX dashboard,

2:18

I downloaded

2:19

the environment

2:19

variables over here,

2:20

and I'm going to go

2:21

ahead and copy this

2:23

file right over to

2:24

our project back

2:27

over in our index JS

2:29

file, we can create

2:30

a new instance of

2:31

the MUX node SDK

2:32

by instantiating

2:33

it and assigning

2:34

it to a variable.

2:35

We'll call this

2:36

variable MUX

2:37

in this case.

2:38

You'll see we're

2:39

loading the mux

2:40

token ID and

2:41

mux token secret

2:42

from the process.

2:43

env object.

2:44

To make sure we're

2:45

appropriately

2:46

loading our env

2:47

file, I'm going

2:48

to use the env

2:49

package to ensure

2:50

we're loading those

2:51

variables correctly.

2:53

So I'll go over to,

2:55

uh, CLI and I'll do

2:56

NPM install and I'll

3:00

just add this little

3:02

config line of code

3:04

here at the top.

3:05

Perfect.

3:06

Now we're ready to

3:06

start interacting

3:07

with the Mux API.

3:08

First thing we want

3:09

to do is create the

3:11

actual live stream.

3:12

So this basically

3:13

acts as a container

3:15

for the live

3:16

stream itself.

3:17

When we create

3:17

a live stream,

3:18

nothing's going

3:19

to go live

3:20

automatically or

3:21

by default, but

3:22

we'll have the

3:22

option to turn that

3:24

live stream on.

3:24

On or off when we

3:26

are ready to publish

3:27

live video to create

3:28

a new live stream.

3:29

It only takes a

3:30

few lines of code.

3:31

We'll provide a

3:32

playback policy,

3:33

which basically says

3:34

anybody can access

3:36

this particular

3:37

live stream by

3:38

calling it a public

3:38

playback policy.

3:40

And we'll also say

3:41

that the new asset

3:42

that's created as

3:43

a result of going

3:45

live can be accessed

3:46

by anyone as well.

3:48

Every live stream

3:48

is automatically

3:49

recorded and

3:50

archived into mux

3:52

so that you can

3:53

play it back at

3:53

a future time.

3:55

Because this is an

3:56

async operation, I'm

3:57

going to wrap it in

3:58

an async function.

4:03

Now the lesson to

4:03

consider is that

4:04

this code will

4:05

typically be running

4:06

on your server as a

4:07

result of a request

4:09

that was made by

4:10

one of your users.

4:11

Maybe they clicked a

4:12

button to say create

4:13

a new live stream

4:15

or perhaps you

4:16

create a live stream

4:16

automatically when

4:17

a new user signs up.

4:19

So this code won't

4:20

typically be running

4:21

in a CLI environment

4:23

but the whole

4:24

interaction process

4:25

is the same idea

4:27

where This code will

4:28

run as a result of,

4:30

uh, your application

4:32

taking action.

4:33

One other small

4:34

refactor to make

4:35

sure that this runs

4:35

appropriately in the

4:37

CLI environment is

4:38

to just change this

4:39

import method to

4:41

let's say, const mux

4:43

equals require node.

4:48

Let's give

4:48

this a shot.

4:50

I'm going to head

4:50

over to the CLI and

4:51

run node index JS.

4:54

And it looks like

4:55

our dot M file

4:56

is not loading

4:57

appropriately.

4:58

So we'll make one

4:59

more change here

4:59

to make sure that

5:00

that gets loaded

5:02

in to make sure

5:03

our environment

5:03

variables get

5:04

picked up.

5:04

I'm going to just

5:05

rename this file

5:06

from the original

5:08

name to simply dot

5:10

M now we should

5:12

be ready to go.

5:13

I'm going to head

5:13

over to the CLI

5:14

and run the script.

5:16

Great.

5:17

You can see that

5:18

our script executed

5:19

successfully.

5:19

We have an

5:20

interesting payload

5:21

that was returned as

5:22

a result of running

5:23

that request.

5:24

This payload

5:24

contains all of the

5:25

information that

5:26

we need to be able

5:28

to turn this live

5:29

stream on and off

5:29

when we're ready.

5:30

To, most

5:31

importantly, is

5:32

the stream key.

5:33

This is basically

5:34

a password that

5:35

allows you to

5:36

connect to this live

5:38

stream container

5:39

and send live

5:40

videos that will

5:41

then be broadcast

5:42

to wherever this

5:43

live stream is

5:44

meant to be played.

5:45

You can also see

5:46

that the live stream

5:47

status is idle

5:48

because we're not

5:48

currently sending

5:49

any live video.

5:50

Let's go ahead

5:50

and change that.

5:51

This stream key

5:52

value is going

5:53

to be commonly

5:53

used between all

5:55

of the different

5:55

live streaming

5:56

applications

5:56

out there,

5:57

whether that's

5:57

OBS, Restream,

5:59

StreamYard, or

6:00

whatever other live

6:01

stream application

6:02

you might be using.

6:03

For this demo, I'm

6:04

going to use OBS.

6:06

I'll copy the stream

6:06

key, head on over

6:08

to OBS, go into the

6:11

preferences, and

6:13

under the stream

6:14

tab, I'll be able to

6:15

choose the service

6:16

that I'm going to be

6:16

streaming through.

6:18

You can find mux

6:19

in the list of

6:19

services in the

6:22

stream key field.

6:23

Simply paste the

6:24

stream key that

6:24

was returned as a

6:25

result of making

6:26

the API request.

6:28

Then click.

6:29

Okay.

6:30

We're all configured

6:31

to start our

6:32

live streaming.

6:32

So I'm going to

6:32

go ahead and click

6:34

start streaming.

6:36

Now let's check out

6:37

the mux dashboard

6:37

and see if we

6:38

can check in on

6:39

our live stream.

6:41

I'm going to go

6:41

into my development

6:42

environment, head

6:43

on over to the

6:44

video tab and click

6:46

on live streams.

6:50

Looks like we

6:50

have one active

6:51

live stream, which

6:52

is good news.

6:53

And there's me in

6:54

that yellow hat

6:56

seems to be working.

6:59

So our live video

7:00

was delivered into

7:01

mugs successfully.

7:02

That's great news,

7:03

but we probably

7:04

aren't going to be

7:04

playing back the

7:05

live stream within

7:06

the MUX dashboard

7:07

every time we want

7:08

our viewers to be

7:09

able to load up

7:10

this live stream

7:11

and watch it as

7:12

we stream live.

7:13

So let me show you

7:14

how to load this

7:15

live stream into

7:16

a player that you

7:17

can then place on

7:18

your own website.

7:19

If you go to

7:19

the Playback

7:20

in Thumbnails

7:21

tab, you'll see

7:22

a Playback ID.

7:23

Now this Playback

7:24

ID is basically the

7:26

reference for this

7:27

livestream that

7:27

allows you to load

7:28

it up in any of

7:30

the video players

7:31

that are out there.

7:32

One of the easiest

7:33

ways to playback

7:34

your livestream is

7:34

by using MuxPlayer.

7:36

You can just copy

7:37

and paste this

7:38

code that's given

7:39

to you right on

7:40

this dashboard page

7:41

and you should be

7:41

able to see this

7:42

livestream load up

7:43

with no problems.

7:44

I'm going to click

7:45

on this little

7:46

clipboard to

7:46

copy this code.

7:47

Head on over to a

7:48

new code sandbox and

7:50

in the body, I'll

7:51

go ahead and paste

7:52

our mux player code.

7:57

Cool.

7:57

And you can see the

7:58

video that we were

7:59

live streaming is

8:00

now appearing right

8:01

on the website.

8:03

So that's it.

8:04

Really with just a

8:04

few lines of code,

8:05

we were able to

8:06

create a new live

8:07

stream and then

8:08

show you how to

8:08

take that stream

8:09

key, plug it into

8:11

your video streaming

8:12

application

8:13

and go live

8:14

without a hitch.

8:15

Then we can take

8:16

that playback ID

8:16

that was created

8:17

and throw it into

8:19

MuxPlayer to watch

8:20

the live stream

8:21

across all the

8:22

different devices

8:23

that are out there.

8:24

Now you're ready

8:24

to get up and

8:25

running with

8:25

your live stream.

8:26

Let us know if you

8:27

have any questions

8:27

along the way.

8:28

We're here to help.