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

@formoe/use-webrtc-connection

v1.0.2

Published

The `useWebRTCConnection` hook provides the logic to create [WebRTC](https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API) connections and ICE candidates for the corresponding peer connection. It takes care of the async processes in the background

Downloads

8

Readme

use-webrtc-connection

The useWebRTCConnection hook provides the logic to create WebRTC connections and ICE candidates for the corresponding peer connection. It takes care of the async processes in the background and translates them to usable react state.

import useWebRTCConnection from "@formoe/use-webrtc-connection"

…

const { openConnection, rtcPeerConnection, addIceCandidate, addIceCandidateErrors, closeConnection } = useWebRTCConnection({ onIceCandidate, connectionConfig })

Hook configuration

The hook takes a configuration object taking a mandatory onIceCandidate callback function that is triggered if the connection provides new candidates. This is mandatory, as it would not make sense to open a connection without ultimately reacting to candidates. As candidates are only send to the peer most of the times the hook does not provide them as state.

connectionConfig is a RTCConfiguration object used when creating a new RTCPeerConnection. You can defined define ice servers here for example.

Returned WebRTC interface

The hook returns some objects and various functions to control the connection process.

The connection

To open a connection you can:

openConnection()

useEffect(() => {
  // open for business
}, [rtcPeerConnection])

The rtcPeerConnection returned by the hook is a RTCPeerConnection object or undefined if no connection was opened. It can also be used to further interact with the connection in case the direct interface is not sufficient.

Evaluating candidates

Sending a candidate

To sucessfully establish a peer connection, the WebRTC layer needs to exchange candidates between the peers.

The callback handed in to the hook via the configuration object is triggered whenever a new RTCIceCandidate is avaialable for the connection:

const onIceCandidate = (candidate) => {
  // signal the peer
}

At this point you don't need to do anything other than sending the candidate to the peer connection. Everything else is taken care of by the WebRTC layer.

Receiving a candidate

If you however retreive a new candidate from your peer, you need to make it know to the connection. The hook makes use of the @formoe/use-multi-async package to deal with this:

const candidateId = addIceCandidate(newCandidate)

addIceCandidate takes a RTCIceCandidate and starts the async process of adding it to the connection. It also returns a Symbol for the candidate. If anything goes wrong with setting it, you can find the correspondng error in the addIceCandidateErrors Array provided by the hook. The entries of the array have the id of their corresponding addIceCandidate call and the error as a JS Error object.

const error = addIceCandidateErrors.find(({id}) => id === candidateId)

if (error) {
  // You might want to notify the user somehow or react to the error.
}

Keep in mind though, that it is not necessarily critical if errors occur. WebRTC negotiation is a constant process throughout the connection and even if adding some candidates results in errors, others might be valid and establish a connection.

The hook keeps only the last 20 errors available.

Hanging up

To manually close an established connection just call:

closeConnection()