Skip to content

Noise Cancellation

Use @cloudflare/realtimekit-noise-cancellation ↗︎ to filter background noise from a participant's microphone. Audio processing runs on their device before sending audio to the meeting.

Noise cancellation supports web applications built with React, Angular, or JavaScript. Mobile SDKs are not supported.

This page is not available for the Mobileplatform.

Installation

Install the package:

npm i @cloudflare/realtimekit-noise-cancellation

Import RealtimeKitNoiseCancellation in your application:

// Skip this import if you use the CDN example.
import { RealtimeKitNoiseCancellation } from "@cloudflare/realtimekit-noise-cancellation";

Initialisation

Pass your initialized meeting to init(). Initialize one noise canceller per meeting instance:

const noiseCanceller = await RealtimeKitNoiseCancellation.init({ meeting });

Keep noise cancellation setup separate from your meeting join or UI flow so participants can join while processing loads.

By default, autoManage is true. The package starts processing when the microphone is available and maintains processing through mute/unmute and reconnections. If the microphone is muted during initialization, processing starts when you unmute it. The package does not unmute the microphone.

Initialisation options

Option Required Description
meeting Yes The RealtimeKit meeting instance.
autoManage No Defaults to true. Automatically starts processing when the microphone is available and disposes the instance when the participant leaves. Set to false to manage activation and cleanup yourself.
onError No Receives runtime and automatic lifecycle errors as an Error.
signal No An AbortSignal that cancels initialization and disposes the instance when aborted.

The package exports the NoiseCancellationOptions TypeScript type for these options.

States

Read enabled to check whether noise cancellation is turned on:

const isEnabled = noiseCanceller.enabled;
Value Meaning
true Noise cancellation is turned on.
false Processing has not started, has been turned off, or the instance has been disposed.

Call getStatus() only while processing is active. It returns a NoiseCancellationStatus object with processing and capture metadata, including sample rates and model packaging:

if (noiseCanceller.enabled) {
	const status = noiseCanceller.getStatus();
	console.log(status);
}

Control methods

Method Description
enable() Starts noise cancellation. The microphone must be active and unmuted.
disable() Stops noise cancellation and restores the microphone's previous processing settings. You can call enable() to start processing again.
dispose() Releases the instance and its resources. Initialize a new instance if you need noise cancellation again.

Each method returns a Promise<void>. Handle rejections from init() and explicit method calls in your application. The onError callback handles runtime and automatic lifecycle errors and does not replace this error handling.

With autoManage: true, the package calls dispose() when the participant leaves. Temporary disconnections do not dispose the instance. Switching to a connected meeting disposes the old instance; initialize a new one for the new meeting object.

Disposing an instance does not stop the microphone track or close RealtimeKit's AudioContext.

Manual handling

Set autoManage: false when your application needs to control when processing starts and when resources are released. Initialization creates the instance without starting noise cancellation or registering automatic cleanup on meeting leave.

  1. Initialize the noise canceller with automatic management turned off:

    const noiseCanceller = await RealtimeKitNoiseCancellation.init({
    	meeting,
    	autoManage: false,
    });
  2. After the microphone becomes active and unmuted, start processing:

    await noiseCanceller.enable();
  3. (Optional) To temporarily stop filtering, call disable(). Call enable() again when the microphone is active and unmuted:

    await noiseCanceller.disable();
  4. When the participant leaves, your application switches meetings, or the component unmounts, release the instance:

    await noiseCanceller.dispose();

You can also pass an AbortController's signal to init() and abort it during application or component teardown. This disposes the instance even when autoManage is false.

Was this helpful?