diff --git a/docs/mediastore/enhancements/interactivity.md b/docs/mediastore/enhancements/interactivity.md deleted file mode 100644 index 7c4e3eea6b..0000000000 --- a/docs/mediastore/enhancements/interactivity.md +++ /dev/null @@ -1,19 +0,0 @@ ---- -sidebar_position: 2 -sidebar_label: Interactivity -title: Stream interactivity -description: Learn how to implement various layers of interactivity between viewers of your streaming content -hide_title: true -keywords: - - momento - - mediastore - - zero buffer rate - - zbr - - streaming - - live - - elemental - - serverless - - metrics - - overlay - - interactivity ---- diff --git a/docs/mediastore/enhancements/live-reactions.md b/docs/mediastore/enhancements/live-reactions.md index c0efff055e..ee72781920 100644 --- a/docs/mediastore/enhancements/live-reactions.md +++ b/docs/mediastore/enhancements/live-reactions.md @@ -1,5 +1,5 @@ --- -sidebar_position: 3 +sidebar_position: 2 sidebar_label: Live reactions title: Live reactions description: Learn how to overlay live reactions on your content with Momento MediaStore @@ -17,3 +17,292 @@ keywords: - overlay - reactions --- + +# Adding real-time emoji reactions to a video stream + +Interactivity plays a significant role in viewer experience. Sending reactions and sharing emotions with other viewers adds a sense of community and engagement you can't find through media streaming alone. In this tutorial, you’ll set up a real-time emoji overlay on a video player. With **Momento Topics**, emoji reactions appear instantly as users interact, allowing for a lively and engaging viewing experience. + +:::info +You can skip straight to a complete [example in GitHub](https://github.com/momentohq/demo-video-streaming) or follow along the tutorial below to build a simple implementation yourself. +::: + +## How it works + +```mermaid +sequenceDiagram + participant Browser + participant Server + participant Momento + + Browser->>+Server: Request auth token + Server->>+Momento: Create auth token + Momento-->>-Server: Return auth token + Server-->>-Browser: Send auth token + + Browser->>+Momento: Publish reaction to Topics + Momento-->>-Browser: Broadcast reaction to all viewers +``` + +First, the browser will request an auth token from a server component called a *token vending machine*, which is responsible for creating and distributing short-lived, limited scope [session tokens](/cache/develop/authentication/tokens). This token will be active for a short period of time and grants access only to the resources needed for sending and receiving reactions. + +After the token is received, the player will publish a message to [Momento Topics](/topics) indicating which emoji was pressed. Topics will broadcast the emoji to all video players that subscribed for reactions. + +The event handler in the browser invoked by Momento Topics then renders the emoji on screen, providing real-time reactions to all your users. + +## Prerequisites + +* A [Momento API key](/cache/develop/authentication/api-keys) + +## Step 1: Building a token vending machine + +The video player needs access to Momento Topics to publish and receive emojis. To grant access, you must generate a session token and return it to the caller. We do this by creating a simple [Express server](https://expressjs.com/) with a `POST /tokens` endpoint. + +```javascript +import express from 'express'; +import { AuthClient, ExpiresIn } from '@gomomento/sdk' + +const authClient = new AuthClient(); +const app = express(); +app.use(express.json()); + +app.post('/tokens', (req, res) => { + const { playerId, streamId} = req.body; + const scope = { + permissions: [{ + role: 'publishsubscribe', + cache: 'video', + topic: streamId + }] + }, + + const tokenResponse = await authClient.generateDisposableToken(scope, ExpiresIn.minutes(30), { tokenId: playerId }); + res.status(201).json({ token: tokenResponse.authToken }); +}); +``` + +The endpoint you created here accepts a request body containing `playerId` and `streamId` properties. The `streamId` is a unique identifier for the video stream being watched. This is used to *limit the scope of the reactions to the requested video*. The `playerId` is the identifier of the caller. Momento best practices say to *always include the identifier of the caller in your session tokens* so you can uniquely identify the actor on your account. We are creating the token with the `playerId` embedded directly in it, which will carry through to every message published by the user. + +:::tip +For the Momento AuthClient to initialize properly, you must have your Momento API key configured in a `MOMENTO_API_KEY` environment variable or pass it directly to the constructor in the `credentialProvider` property. +::: + +The permissions granted in the token allow the caller to both *publish* and *subscribe* to a topic dedicated to the video stream the caller is watching. This topic lives in a [cache](/cache) named `video`. + +Upon success, this endpoint will return a `201 Created` status code along with the generated token that expires in 30 minutes. + +## Step 2: Creating the video player and emoji overlay + +The emojis sent to each other by viewers are not actually part of the embedded video player itself. In reality, the emojis are overlayed in a `div` surrounding the video player. Emojis are placed with *absolute* positioning and animated with keyframes in CSS. + +```html + + +
+ +