Building a Serverless Live-streaming Platform with React & AWS
Tags: awsamplify, aws, serverless, livestreaming
Published: 2020-03-26 13:19:52

---
title: Building a Serverless Live-streaming Platform with React & AWS
published: true
description: How to stream from OBS or your favorite streaming engine using AWS and Amplify Video
tags: aws amplify, aws, serverless, live streaming
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/gnl9o645d91krqjxacm1.jpg
---

> Cover image by [Caspar Camille Rubin](https://unsplash.com/@casparrubin)

Live streaming is becoming more and more popular. Platforms like Twitch and YouTube allow you to stream using tools like OBS directly to your audience in real-time from your desktop or laptop.

What if you wanted to build your own custom streaming platform embedded directly into your own website or app? In this post, I'll show you how to do this in just a few steps using Amplify Video.

The main tutorial will cover how to do this with a React Front end, but I'll also show how to do this with React Native.

> Unlike most other Amplify services, the video services used in this tutorial do not fall under the free tier. Be sure to stop your video stream and delete the project when you are done if you are not using this in production. See pricing for these services [here](https://dev.to/aws/building-a-serverless-live-streaming-platform-with-react-aws-1jmk#pricing)

__To view the completed project, click [here](https://github.com/dabit3/react-aws-live-streaming)__

> This tutorial shows you how to build the streaming platform itself. If you're interested in embedding your existing Twitch, YouTube, or Mixer stream in your website, check out the [react-stream](https://github.com/ryancharris/react-livestream) package from [Ryan Harris](https://twitter.com/ryan_c_harris)

## Overview

This post shows you how to use the [Amplify CLI](https://aws-amplify.github.io/docs/cli-toolchain/quickstart), [AWS Elemental](https://www.elemental.com/), and [Amplify Video](https://github.com/awslabs/amplify-video) (an Amplify CLI plugin by [Sam Patzer](https://twitter.com/Wizages)) to deploy a video streaming service embeddable into any website or app.

We'll then look at how to embed the video player into a React app and test it out.

Under the hood, Amplify Video uses [AWS Media Services](https://aws.amazon.com/media-services/) like [Elemental MediaLive](https://aws.amazon.com/medialive/?c=ms&sec=srv) and [Elemental MediaStore](https://aws.amazon.com/mediastore/?c=ms&sec=srv), but because the CLI does all of the heavy lifting you will only have to work locally from your CLI to set everything up.

Elemental MediaLive is a fully managed and Serverless service that allows you to encode live video for broadcast and streaming to any device. It lets you create high-quality video streams for delivery to broadcast televisions and internet-connected multiscreen devices, like connected TVs, tablets, smart phones, and set-top boxes. The service works by encoding your live video streams in real-time, taking a larger-sized live video source and compressing it into smaller versions for distribution to your viewers.

This tutorial should take a total of about 15 minutes from start to finish.

> To follow along and test out the completed app in this tutorial, you will need have [OBS](https://obsproject.com/download) installed

### Getting started

The first think you should do is install the latest version of the Amplify CLI and configure it with a user if you have not already done so:

```sh
$ npm i -g @aws-amplify/cli

$ amplify configure
```

> For a video walkthrough of how to configure the Amplify CLI, click [here](https://www.youtube.com/watch?v=fWbM5DLh25U)

Next, install the [Amplify Video](https://github.com/awslabs/amplify-video) CLI plugin:

```sh
$ npm i -g amplify-category-video
```

Now that the Amplify dependencies are set up on your computer, create a new React project and install Video.js:

```sh
$ npx create-react-app my-streaming-app

$ cd my-streaming app

$ npm install video.js
```

### Creating the Amplify project

From within the root of the project, initialize a new Amplify app:

```sh
$ amplify init

# Choose the project name, environment name, your preferred text editor, and the defaults for the rest of the options.

# When prompted for your AWS Profile, choose the profile you created when you configured the Amplify CLI
```

### Add a video resource

Now that the Amplify project has been configured, we can add a video resource!

To do so, we will run the Amplify `add` command:

```sh
$ amplify add video

# Follow the prompts. Note that the default values will be sufficient for this tutorial.
```

To deploy the service, run the Amplify `push` command:

```sh
$ amplify push
```

> When deployed, the streaming service will automatically be started. To turn it off, either run `amplify video stop` choosing __Yes__ to all prompts, or run `amplify delete` to delete the project. Be sure not to leave the stream running when you are not using it as you will be billed for the usage.

### Adding the video player to the React application

Next, open __src/App.js__ and add the following:


```js
import React from 'react'
import videojs from 'video.js'
import awsvideoconfig from './aws-video-exports'
import './App.css'
import 'video.js/dist/video-js.css'

class VideoPlayer extends React.Component {
  componentDidMount() {
    this.player = videojs(this.videoNode, this.props)
  }

  componentWillUnmount() {
    if (this.player) {
      this.player.dispose()
    }
  }

  render() {
    return (
      <>
        <div data-vjs-player style={{
            width: 960, height: 540
          }}>
          <video  ref={(node) => { this.videoNode = node; }} className="video-js" />
        </div>
      </>
    );
  }
}

const videoJsOptions = {
  autoplay: true,
  controls: true,
  sources: [{
    src: awsvideoconfig.awsOutputLiveLL,
  }]
}

function App() {
  return (
    <div>
      <nav style={nav}>
        <p style={navHeading}>Live Streaming with React & AWS</p>
      </nav>
      <div style={container}>
        <VideoPlayer { ...videoJsOptions } />
      </div>
    </div>
  );
}

const nav = { padding: '0px 40px', height: 60, borderBottom: '1px solid #ddd', display: 'flex', alignItems: 'center' }
const container = { paddingTop: 40, width: 960, margin: '0 auto' }
const navHeading = { margin: 0, fontSize: 18 }

export default App
```

### Enable OBS

Open Broadcast Software (OBS) can be automatically configured through Amplify video. Download OBS, make sure it's closed, and run the following command to configure an OBS profile to push to your Video resource.

```sh
$ amplify video setup-obs
```

### Running the app

To run the app, run the `start` command:

```sh
$ npm start
```

### Stopping the stream

When you are done with your stream, run the `stop` command to shut down your stream so you do not incur any additional streaming charges.

```sh
$ amplify video stop

? overwrite .../mylivestream-livestream-workflow.template: Y
? Would you like to overwrite all files? Y
? Are you sure you want to continue? Y
? overwrite ./src/aws-video-exports.js? Y
```

When the stream is turned off, it will be in `Idle` state. In Elemental MediaLive, the `Idle` state costs a little under 0.01 per hour. If you'd like to no incur any charges at all, delete the services with `amplify delete`.

### Restarting the stream

If you'd like to turn the stream back on, run the following commands.

```sh
$ amplify video start

? overwrite .../mylivestream-livestream-workflow.template: Y
? Would you like to overwrite all files? Y
? Are you sure you want to continue? Y
? overwrite ./src/aws-video-exports.js? Y
```

### Deleting the services

To delete the services from your account, run the `delete` command:

```sh
$ amplify delete
```

## React Native

Amplify Video also works with React Native. To implement the stream on the React Native client, first install [React Native Video](https://github.com/react-native-community/react-native-video#readme)

```sh
npm install react-native-video
```

To link the native dependencies, follow the guide [here](https://github.com/react-native-community/react-native-video#installation).

Next, set the video URI as the MediaStore URL

```js
import Video from 'react-native-video';

<Video source={{uri: awsvideoconfig.awsOutputLiveLL}}
  ref={(ref) => {
    this.videoNode = ref
  }}
/>
```

### Pricing

To view pricing for MediaStore, click [here](https://aws.amazon.com/mediastore/pricing/).

To view pricing for MediaLive, click [here](https://aws.amazon.com/medialive/pricing/)