Skip to content
We've just launched - check out our docs! đź“—
  • React
  • Stream
  • useCreateStream

useCreateStream

Hook for creating a new Stream.

import { useCreateStream } from '@livepeer/react';

Usage

đź’ˇ

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.

import { useCreateStream } from '@livepeer/react';
 
const streamName = `New Stream`;
 
function App() {
  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 React 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.

import { useCreateStream } from '@livepeer/react';
 
function App() {
  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. `public` indicates no access control will be applied (anyone
   * with the `playbackId` can view without a JWT).
   */
  type: 'jwt' | 'public';
};

For more details on the playback policy, see the Access Control example.

mutationConfig

The mutationConfig parameter allows for any React Query useMutation options, such as cacheTime or retry. These override any configs passed by default by the internal hook.

import { useCreateStream } from '@livepeer/react';
 
function App() {
  const { mutate: createStream } = useCreateStream({
    name: 'My new stream',
    mutationConfig: { retry: 3 },
  });
}