useCreateStream
React Hook for creating a new stream.
Hook for creating a new Stream.
Usage
import { useCreateStream } from '@livepeer/react';
The stream ID should only be passed to users who are allowed to modify the
stream. Ensure viewers are only provided with the playbackId
, which is a
limited identifier that provides only the ability to stream media using the
Player
.
The following example shows how a stream can be created.
const streamName = `New Stream`;
function SomeComponent() {
const {
mutate: createStream,
data: stream,
status,
} = useCreateStream({ name: streamName });
return (
<div>
<button
disabled={status === "loading" || !createStream}
onClick={() => createStream?.()}
>
Create Stream
</button>
{stream && <div>Stream Key: {stream.streamKey}</div>}
</div>
);
}
Return Value
The return value is partially based on Tanstack Query, with some return types aggregated for simplicity.
{
data?: Stream,
error?: Error,
isError: boolean,
isIdle: boolean,
isLoading: boolean,
isSuccess: boolean,
status: 'idle' | 'loading' | 'success' | 'error',
mutate: () => void,
mutateAsync: () => Promise<Stream>,
variables?: CreateStreamArgs
}
Configuration
name
The name for the new stream. Required to create a stream.
function SomeComponent() {
const { mutate: createStream } = useCreateStream({
name: "My new stream",
});
}
profiles
Transcoding profiles to use for the stream for ABR playback. When this is not
defined, the defaultTranscodingProfiles
are used (720p, 480p, and 360p).
record
Whether to create recordings of the stream sessions. Defaults to false
.
multistream
The configuration for multistreaming (AKA “restream” or “simulcast”) - allows configuration of targets where this stream should be simultaneously streamed to.
playbackPolicy
Configuration for stream playback access-control policy. Defaults to public
.
type PlaybackPolicy = {
/**
* The type of playback policy to apply. `jwt` requires a signed JWT for
* playback. `webhook` requires that a webhook is configured and passed during the
* creation of the asset. `public` indicates no
* access control will be applied (anyone with the `playbackId` can
* view without a JWT or webhook).
*/
type: "webhook" | "jwt" | "public";
};
type WebhookPlaybackPolicy<TContext extends object> = PlaybackPolicy & {
type: "webhook";
/** The ID of the webhook which has already been created. */
webhookId: string;
/** The context which is passed to the webhook when it is called on playback. */
webhookContext: TContext;
};
mutationConfig
The mutationConfig
parameter allows for any
Tanstack Query
useMutation
options, such as cacheTime
or retry
. These override any
configs passed by default by the internal hook.
function SomeComponent() {
const { mutate: createStream } = useCreateStream({
name: "My new stream",
mutationConfig: { retry: 3 },
});
}
Was this page helpful?