For the next step please check our Storm JavaScript Library - Stream & Server guide where you’ll learn how to connect library with Storm Streaming Cloud or Server instance.
In this tutorial, you will learn how to correctly use the Storm Library React Component to embed a library on websites built with the ReactJS framework.
npm install @stormstreaming/stormlibrary-react
yarn add @stormstreaming/stormlibrary-react
import {
useEffect,
useRef
} from "react";
import {
StormLibrary,
StormStreamConfig,
StormLibraryEvent,
StormLibraryClass,
} from "@stormstreaming/StormLibrary-react";
const streamConfig: StormStreamConfig = {
stream: {
serverList: [{
host: "localhost",
application: "live",
port: 8081,
ssl: false
}],
streamKey: "test"
},
settings: {
autoStart: true
video: {
containerID: "videoElement"
}
},
};
function App() {
const instanceRef = useRef<StormLibraryClass>(null);
useEffect(() => {
// Sample callback for event listener
const onPlaybackProgress = (event: StormLibraryEvent["playbackProgress"]) => {
console.log(`Player ID: ${event.ref.getLibraryID()} - Playback Progress Update`);
console.log(`-- >: streamStartTime (source): ${event.streamStartTime}`);
console.log(`-- >: streamDuration (source): ${event.streamDuration}`);
console.log(`-- >: playbackStartTime (this stream): ${event.playbackStartTime}`);
console.log(`-- >: playbackDuration (this stream): ${event.playbackDuration}`);
};
if (instanceRef.current) {
// Register event listener
instanceRef.current.addEventListener("playbackProgress", onPlaybackProgress);
// Initialize player (all event-listeners are now registered)
instanceRef.current.initialize();
}
return () => {
if (instanceRef.current) {
instanceRef.current.removeEventListener("playbackProgress", onPlaybackProgress);
}
};
}, []);
return (
<div>
<StormPlayer
ref={libraryRef}
streamConfig={streamConfig}
/>
</div>
);
}
export default App;
import {
StormLibrary,
StormStreamConfig,
StormLibraryEvent,
StormLibraryClass
} from "@stormstreaming/StormLibrary-react";
These are the required imports that include Storm Library into your class, along with the configuration object.
const streamConfig: StormStreamConfig = {
stream: {
serverList: [{
host: "localhost",
application: "live",
port: 8081,
ssl: false
}],
streamKey: "test"
},
settings: {
autoStart: true
video: {
containerID: "videoElement"
}
},
};
The stream configuration object contains parameters related to the video transmission itself. Similarly to the example presented above, it includes only basic options. You can learn more about configuring this section from the Storm Library - Stream & Server Configuration Guide.
useEffect(() => {
// Sample callback for event listener
const onPlaybackProgress = (event: StormLibraryEvent["playbackProgress"]) => {
console.log(`Player ID: ${event.ref.getLibraryID()} - Playback Progress Update`);
console.log(`-- >: streamStartTime (source): ${event.streamStartTime}`);
console.log(`-- >: streamDuration (source): ${event.streamDuration}`);
console.log(`-- >: playbackStartTime (this stream): ${event.playbackStartTime}`);
console.log(`-- >: playbackDuration (this stream): ${event.playbackDuration}`);
};
if (instanceRef.current) {
// Register event listener
instanceRef.current.addEventListener("playbackProgress", onPlaybackProgress);
// Initialize player (all event-listeners are now registered)
instanceRef.current.initialize();
}
return () => {
if (instanceRef.current) {
instanceRef.current.removeEventListener("playbackProgress", onPlaybackProgress);
}
};
}, []);
In the above example, we can see how to add an event listener to the library. The lists of supported events can be found in the Library Events and Playback Events sections. For a comprehensive description of the API, please refer to the API Methods section.
instanceRef.current.initialize();
Once all event listeners have been registered with the library, we can ultimately initialize the library using the initialize() method. Please bear in mind that if the library is set to operate in autostart mode and events are registered after this method is called, your code may not function correctly due to the asynchronous execution of the code.
Create a free ticket and our support team will provide you necessary assistance.