npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-twilio-video-hooks

v0.0.1

Published

A library of React hooks to make working with twlio-video easier.

Downloads

2

Readme

use-twilio-video-hooks

A library of React hooks to make working with twlio-video easier.

NPM JavaScript Style Guide

Acknowledgements

This repository is an unofficial extension of use-twilio-video by Nur Latifah Ulfah. We'd like to express our gratitude for the foundational work they provided, which greatly assisted in the development of this project. Please visit the original repository to appreciate the great work they started with.

Features

  • Connect and disconnect from a room.
  • Toggle camera mute.
  • Toggle microphone mute.
  • Identify the dominant speaker.

Install

npm install --save twilio-video use-twilio-video-hooks

Prerequisite

You need Twilio Access Token to use Twilio Video. You can use Testing Tools in the Twilio Console to generate the token. You can read the guide here.

Or you can also follow the next instruction while running the sample project in folder example. To run example:

  1. From root repo directory, change directory to example folder: cd example
  2. Install dependencies: npm install
  3. Copy .env.example and rename to .env
  4. Add your Twilio API Key to the .env file. You can get your API Key from your Twilio account. You can follow this guide.
  5. Run server to generate token: npm run server
  6. Open different tab and run the react app: npm start

Usage

There are two main hooks, useRoom and useTrack.

useRoom is used to manage room state. In Twilio, a Room represents a real-time audio, data, video, and/or screen-share session, and is the basic building block for a Programmable Video application.

useTrack is used to manage tracks in a room. In Twilio, Tracks represent the individual audio, data, and video media streams that are shared within a Room. This tracks are shared by Participants. Participants represent client applications that are connected to a Room and sharing audio, data, and/or video media with one another.

The following are the minimum components needed to be able to create a simple video call application using twilio and use-twilio-video-hooks.

  1. Create a component that represent a Room.

    // Room.js
    import Participant from './Participant'
    import { useRoom } from 'use-twilio-video-hooks'
    
    function Room ({ token, roomName }) {
      const { room, error, connectRoom, disconnectRoom, localParticipant, remoteParticipants, dominantSpeaker, isCameraOn, toggleCamera, isMicrophoneOn, toggleMicrophone } = useRoom()
    
    
      useEffect(() => {
        if (!room && token && roomName) {
          connectRoom({ token, options: { name: roomName, dominantSpeaker: true } })
          return () => disconnectRoom()
        }
      }, [connectRoom, disconnectRoom, room, roomName, token])
    
      // ... other
    
      // usage example in simple component
      if (room)
        return (
          <div>
            <div>
              <button onClick={() => disconnectRoom()}>disconnect</button>
              <button onClick={() => toggleCamera()}>
                {isCameraOn ? 'turn off camera' : 'turn on camera'}
              </button>
              <button onClick={() => toggleMicrophone()}>
                {isMicrophoneOn ? 'turn off mic' : 'turn on mic'}
              </button>
            </div>
    
            <div>Local participant: {JSON.stringify(localParticipant?.identity)}</div>
            <Participant participant={localParticipant} />
    
            <div>
              Remote participants:{' '}
              {JSON.stringify(remoteParticipants.map(v => v.identity))}
            </div>
    
            <div>Dominant speaker: {JSON.stringify(dominantSpeaker?.identity)}</div>
    
            <div>
              {remoteParticipants.map(p => (
                <Participant participant={p} />
              ))}
            </div>
          </div>
        )
    
      // ... other
    }
  2. Create a component represent Participants.

    // Participant.js
    import AudioTrack from './AudioTrack'
    import VideoTrack from './VideoTrack'
    import { useTrack } from 'use-twilio-video-hooks'
    
    function Participant ({ participant }) {
      const { videoOn, audioOn, videoTrack, audioTrack } = useTrack({ participant })
    
      return (
        <div>
          {videoOn ? <VideoTrack track={videoTrack} /> : 'video off'}
          <br />
          {audioOn ? <AudioTrack track={audioTrack} /> : 'audio off'}
        </div>
      )
    }
    
    export default Participant
  3. Create two components, to attach participants' video and audio

    // VideoTrack.js
    import { useEffect, useRef } from 'react'
    
    export default function VideoTrack ({ track }) {
      const ref = useRef()
    
      useEffect(() => {
        if (track) {
          const el = ref.current
          track.attach(el)
    
          return () => {
            track.detach(el)
          }
        }
      }, [track])
    
      return <video style={{ maxWidth: '100%' }} ref={ref} />
    }
    
    // AudioTrack.js
    import { useEffect, useRef } from 'react'
    
    export default function AudioTrack ({ track }) {
      const ref = useRef()
    
      useEffect(() => {
        if (track) {
          const el = ref.current
          track.attach(el)
    
          return () => {
            track.detach(el)
          }
        }
      }, [track])
    
      return <audio ref={ref} />
    }

License

MIT © Jay Mathew