### Installing TypeScript Example Dependencies Source: https://github.com/100mslive/server-sdks/blob/main/examples/nodejs/README.md Installs the necessary global packages (ts-node, typescript, @types/node) required to run the TypeScript example. ```Shell npm install -g ts-node typescript '@types/node' ``` -------------------------------- ### Running CommonJS Example Source: https://github.com/100mslive/server-sdks/blob/main/examples/nodejs/README.md Executes the CommonJS JavaScript example (`index.cjs`) using yarn. ```Shell yarn cjs ``` -------------------------------- ### Running ES Module Example Source: https://github.com/100mslive/server-sdks/blob/main/examples/nodejs/README.md Executes the ES Module JavaScript example (`index.js`) using yarn. ```Shell yarn esm ``` -------------------------------- ### Running TypeScript Example Source: https://github.com/100mslive/server-sdks/blob/main/examples/nodejs/README.md Executes the TypeScript example (`index.ts`) using ts-node via yarn. ```Shell yarn ts ``` -------------------------------- ### Installing 100ms Server SDK via NPM Source: https://github.com/100mslive/server-sdks/blob/main/nodejs/README.md Instructions for installing the 100ms server-side SDK package using the npm package manager. This command adds the package as a dependency to your project. ```Shell npm install --save @100mslive/server-sdk ``` -------------------------------- ### Installing 100ms Server SDK via Yarn Source: https://github.com/100mslive/server-sdks/blob/main/nodejs/README.md Instructions for installing the 100ms server-side SDK package using the Yarn package manager. This command adds the package as a dependency to your project. ```Shell yarn add @100mslive/server-sdk ``` -------------------------------- ### Listing Room Codes using 100ms Server SDK API (JavaScript) Source: https://github.com/100mslive/server-sdks/blob/main/nodejs/README.md Provides an example of using the `hms.api` property to fetch a specific room object by its ID. It illustrates importing the SDK, initializing it, making a GET request to the `rooms/{roomId}` endpoint, and logging the returned room object. ```JavaScript import HMS from "@100mslive/server-sdk"; const hms = new HMS.SDK(); /** * cURL call: * `curl --location --request GET 'https://api.100ms.live/v2/rooms/' --header 'Authorization: Bearer '` */ const params = {}; // the request's body goes here // appropriate header configuration (incl. management token authorization) is handled internally const room = await hms.api.get(`rooms/${roomId}`, params); // `room` contains the room object, mapped from response's body console.log(room); ``` -------------------------------- ### Making Generic API Calls with 100ms Server SDK (JavaScript) Source: https://github.com/100mslive/server-sdks/blob/main/nodejs/README.md Demonstrates how to use the `hms.api` property to make generic GET requests to 100ms endpoints not yet explicitly supported by the SDK. It shows importing the SDK, initializing it, making a GET call with a path and parameters, and logging the result. ```JavaScript import HMS from "@100mslive/server-sdk"; const hms = new HMS.SDK(); const hmsObject = await hms.api.get(path, params); console.log(hmsObject); ``` -------------------------------- ### Using Active Room APIs in JavaScript Source: https://github.com/100mslive/server-sdks/blob/main/nodejs/README.md Provides examples of interacting with active rooms using the 100ms SDK. It demonstrates how to retrieve a list of peers currently in an active room and how to send a broadcast message to all participants in that room. ```JavaScript import HMS from "@100mslive/server-sdk"; const hms = new HMS.SDK(); // list peers in active room - const peers = await hms.activeRooms.retrieveActivePeers(roomId); console.log(peers); // send broadcast message to all peers - await hms.activeRooms.sendMessage(roomId, { message: "test" }); ``` -------------------------------- ### Manage Rooms using 100ms SDK (JS) Source: https://github.com/100mslive/server-sdks/blob/main/nodejs/README.md This example shows how to create a new room, including creating one with specific options, and how to update an existing room's properties. It requires initializing the 100ms SDK and uses the 'hms.rooms' methods, taking optional creation options or the room ID and update options. ```javascript import HMS from "@100mslive/server-sdk"; const hms = new HMS.SDK(); // creating a room - const room = await hms.rooms.create(); // with room options - const roomCreateOptions = { name, description, template_id, recording_info, region, }; const roomWithOptions = await hms.rooms.create(roomCreateOptions); // updating a room - const roomUpdateOptions = { name }; const updatedRoom = await hms.rooms.update(room.id, roomUpdateOptions); console.log(room, roomWithOptions, updatedRoom); ``` -------------------------------- ### Configuring 100ms Server SDK Credentials via Environment Variables Source: https://github.com/100mslive/server-sdks/blob/main/nodejs/README.md Example of setting environment variables (`HMS_ACCESS_KEY` and `HMS_SECRET`) to provide credentials for the 100ms SDK. The SDK can automatically pick up these variables if no credentials are passed during initialization. ```Shell HMS_ACCESS_KEY=accessKey123 // access key HMS_SECRET=secret456 // app secret ``` -------------------------------- ### Using Analytics APIs in JavaScript Source: https://github.com/100mslive/server-sdks/blob/main/nodejs/README.md Shows how to access analytics data using the 100ms SDK. Examples include iterating through track events filtered by room ID and type, and listing recording events with similar filtering capabilities. ```JavaScript // list track events by room id and type - const trackEventFilters: HMS.Analytics.TrackEvent.FilterParams = { room_id: "roomId", type: ["track.add.success", "track.remove.success"], }; const trackEventsIterable = hms.analytics.listTrackEvents(trackEventFilters); for await (const trackEvent of trackEventsIterable) { console.log(trackEvent); } // list recording events by room id and type - const recordingEventFilters: HMS.Analytics.RecordingEvent.FilterParams = { room_id: "roomId", type: "beam.recording.success", }; const recordingEventsIterable = hms.analytics.listRecordingEvents(recordingEventFilters); for await (const recordingEvent of recordingEventsIterable) { console.log(recordingEvent); } ``` -------------------------------- ### Understand 100ms SDK Error Structure (TS/JS) Source: https://github.com/100mslive/server-sdks/blob/main/nodejs/README.md This section defines the interface for SDK exceptions, outlining the properties included when an error occurs. It also provides a concrete JavaScript example of an error object following this structure, showing typical values for code, name, and message. ```typescript interface SDKException { code?: number; name: string; message: string; } ``` ```javascript e.g. const hlsErr = { code: 404, name: "Not Found", message: "hls not running", }; ``` -------------------------------- ### Manage Live Streams and Metadata using 100ms SDK (JS) Source: https://github.com/100mslive/server-sdks/blob/main/nodejs/README.md This example shows how to retrieve a live stream object by its ID and then send timed metadata to that specific live stream. It utilizes the 'hms.liveStreams' methods, requiring the stream ID for retrieval and the stream ID along with payload and duration for sending metadata. ```javascript // get a live stream object by its stream id - const liveStream = await hms.liveStreams.retrieve("streamID"); // send timed metadata to that live stream - const timedMetadataParams: HMS.LiveStream.TimedMetadataParams = { payload: "Hello, this is the message", duration: 5000, }; const sameLiveStream = await hms.liveStreams.sendTimedMetadata(liveStream.id, timedMetadataParams); console.log(liveStream, sameLiveStream); ``` -------------------------------- ### Manage Room Recordings using 100ms SDK (JS) Source: https://github.com/100mslive/server-sdks/blob/main/nodejs/README.md This example illustrates how to retrieve a specific recording object by its ID and conditionally stop it if it belongs to a particular room. It also shows how to stop all recordings associated with a given room ID using the 'hms.recordings' methods. ```javascript // check a recording's room id and stop it - const recording = await hms.recordings.retrieve("objectID"); if (recording.room_id == "roomID") { hms.recordings.stop(recording.id); } else { // stop all recordings in that room - const stoppedRecordings = await hms.recordings.stopAll("roomID"); console.log(stoppedRecordings); } ``` -------------------------------- ### Manage External Streams using 100ms SDK (JS) Source: https://github.com/100mslive/server-sdks/blob/main/nodejs/README.md This snippet demonstrates how to initiate a new external stream for a room and subsequently stop an existing external stream using its ID. It requires the 100ms server SDK initialized as 'hms'. The start operation takes meeting URL and RTMP URLs, while the stop operation takes the stream ID. ```javascript // start a new external stream - const externalStreamStartParams: HMS.ExternalStream.StartParams = { meeting_url: "meetingURL", rtmp_urls: ["rtmpURL1", "rtmpURL2"], }; const newExternalStream = await hms.externalStreams.start("roomId", externalStreamStartParams); // stop an external stream by id - const stoppedExternalStream = await hms.externalStreams.stop(newExternalStream.id); console.log(newExternalStream, stoppedExternalStream); ``` -------------------------------- ### Initializing SDK and Creating Room in TypeScript Source: https://github.com/100mslive/server-sdks/blob/main/nodejs/README.md Demonstrates initializing the 100ms SDK and creating a room using TypeScript, showing how to import and use SDK types for room creation options and the room object. Requires the SDK to be configured with credentials. ```TypeScript import HMS from "@100mslive/server-sdk"; const hms = new HMS.SDK(); // create a room with options - let roomWithOptions: HMS.Room; const roomCreateOptions: HMS.Room.CreateOptions = { name, description, template_id, recording_info, region, }; roomWithOptions = await hms.rooms.create(roomCreateOptions); ``` -------------------------------- ### Initializing 100ms Server SDK in JavaScript Source: https://github.com/100mslive/server-sdks/blob/main/nodejs/README.md Shows how to create a new instance of the 100ms SDK. The SDK can be initialized by passing the access key and secret directly or by relying on environment variables (`HMS_ACCESS_KEY`, `HMS_SECRET`). ```JavaScript const hms = new HMS.SDK(accessKey, secret); // OR const hms = new HMS.SDK(); // Credentials are in env variables ``` -------------------------------- ### Importing 100ms Server SDK in JavaScript Source: https://github.com/100mslive/server-sdks/blob/main/nodejs/README.md Demonstrates how to import the 100ms server-side SDK library in a JavaScript project using ES module syntax or commonjs-like import. This is the first step before initializing the SDK. ```JavaScript import HMS from "@100mslive/server-sdk"; // OR import * as HMS from "@100mslive/server-sdk"; ``` -------------------------------- ### Generating Auth Token for Client SDKs in JavaScript Source: https://github.com/100mslive/server-sdks/blob/main/nodejs/README.md Shows how to use the 100ms SDK to generate an authentication token required by client-side SDKs to join a room. The token can be generated with minimal configuration or with additional options like validity period. ```JavaScript import HMS from "@100mslive/server-sdk"; const hms = new HMS.SDK(); const tokenConfig = { roomId, role, userId }; console.log(await hms.auth.getAuthToken()); // with additional token options - const additionalTokenConfig = { roomId, role, userId, issuedAt, notValidBefore, validForSeconds, }; console.log(await hms.auth.getAuthToken(additionalTokenConfig)); ``` -------------------------------- ### Access Recording Assets using 100ms SDK (JS) Source: https://github.com/100mslive/server-sdks/blob/main/nodejs/README.md This snippet demonstrates how to fetch a recording asset object using its unique ID and then generate a pre-signed URL to securely access the recorded content. It uses the 'hms.recordingAssets' methods, requiring the asset ID for both retrieval and URL generation. ```javascript // get a recording asset by id - const recordingAsset = await hms.recordingAssets.retrieve("assetId"); // generate a pre-signed URL to access that - const preSignedURL = await hms.recordingAssets.generatePreSignedURL(recordingAsset.id); console.log("URL: " + preSignedURL.url); console.log("Path: " + preSignedURL.path); ``` -------------------------------- ### Retrieve Session Information using 100ms SDK (JS) Source: https://github.com/100mslive/server-sdks/blob/main/nodejs/README.md This snippet demonstrates how to list sessions associated with a specific room using pagination filters and how to retrieve the currently active session in a room. It requires initializing the 100ms SDK and uses the 'hms.sessions' methods, handling potential errors when no active session is found. ```javascript import HMS from "@100mslive/server-sdk"; const hms = new HMS.SDK(); // list sessions associated with a specific room - const sessionFilters = { room_id: "test_room_id", limit: 10, // specifies the max no. of objects in one page // this means `iterable.isNextCached` will be `false` once every 10 times }; const sessionsByRoomIterable = hms.sessions.list(sessionFilters); for await (const session of sessionsByRoomIterable) { console.log(session); if (!allSessionsIterable.isNextCached) { console.log("the next loop is gonna take some time"); } } // get the active session in a room - try { const activeSessionInRoom = hms.sessions.retrieveActiveByRoom("roomId"); console.log(activeSessionInRoom); } catch (error) { console.log("No active session found in the room!"); } ``` -------------------------------- ### Manage Room Codes using 100ms SDK (JS) Source: https://github.com/100mslive/server-sdks/blob/main/nodejs/README.md This snippet demonstrates how to create new room codes for a specified room and then disable one of the generated codes. It requires initializing the 100ms SDK and uses the 'hms.roomCodes' methods, taking the room ID for creation and the room code and a boolean flag for enabling/disabling. ```javascript import HMS from "@100mslive/server-sdk"; const hms = new HMS.SDK(); // create room codes - const roomCodesForRoom = await hms.roomCodes.create(roomId); console.log(roomCodesForRoom); // disable a room code - const disabledRoomCode = await hms.roomCodes.enableOrDisable(roomCodesForRoom[0].code, false); console.log(disabledRoomCode); ``` -------------------------------- ### Generating 100ms Management Token with Server SDK (JavaScript) Source: https://github.com/100mslive/server-sdks/blob/main/nodejs/README.md Shows how to use the `hms.auth.getManagementToken()` method from the 100ms server SDK to generate a management token for API authorization. It demonstrates generating a token without options and with optional configuration like validity period. ```JavaScript import HMS from "@100mslive/server-sdk"; const hms = new HMS.SDK(); console.log(await hms.auth.getManagementToken()); // with token options - const tokenConfig = { issuedAt, notValidBefore, validForSeconds, }; console.log(await hms.auth.getManagementToken(tokenConfig)); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.