Skip to main content

Class: Publisher

Publishes live media to Vindral LiveCloud over MoQ.

The Publisher handles encoding, connection management, and automatic reconnection. Use .on("event", callback) to listen to events. See PublisherEvents for available events.

const publisher = new Publisher({
url: "https://moq.global.cdn.vindral.com:7001/voq/publish",
channelId: "my-channel",
authToken: "secret",
})

publisher.on("connection state", (state) => console.log("connection:", state))
publisher.on("error", (error) => console.error(error))
publisher.on("health", (health) => console.log("health:", health.state))

publisher
.addTrack({ kind: "video", track: videoTrack, config: videoConfig })
.addTrack({ kind: "audio", track: audioTrack, config: audioConfig })

await publisher.publish()

// Later: stop publishing but keep encoding active
await publisher.unpublish()

// Or: shut down completely
await publisher.close()

Extends​

Accessors​

connectionState​

Get Signature​

get connectionState(): ConnectionState

Current connection state. Returns "disconnected" when no connection exists.

Returns​

ConnectionState


health​

Get Signature​

get health(): BroadcastHealth

Latest broadcast health (read-only live result); "unknown" until the first tick and after unpublish/close.

Returns​

BroadcastHealth


state​

Get Signature​

get state(): PublisherState

Current publisher state.

Returns​

PublisherState

Methods​

addTrack()​

addTrack(input): this

Add a video or audio track for encoding. Can only be called in "idle" state.

Parameters​

input​

TrackInput

Returns​

this

this for method chaining


close()​

close(): Promise<void>

Close the publisher completely. Works from any state. Terminal, cannot be reused.

Returns​

Promise<void>


closed()​

closed(): Promise<PublisherError | undefined>

Promise that resolves when the publisher is closed, with the error that caused it (if any).

Returns​

Promise<PublisherError | undefined>


hasListeners()​

hasListeners<T>(eventName): boolean

Check whether there are any listeners registered for the given event.

Type Parameters​

T​

T extends keyof PublisherEvents

Parameters​

eventName​

T

Returns​

boolean

Inherited from​

Emitter.hasListeners


off()​

Call Signature​

off<T>(eventName, fn): void

Remove an event listener from eventName

Type Parameters​
T​

T extends never

Parameters​
eventName​

T

fn​

() => EventListenerReturnType

Returns​

void

Inherited from​

Emitter.off

Call Signature​

off<T>(eventName, fn): void

Remove an event listener from eventName

Type Parameters​
T​

T extends keyof PublisherEvents

Parameters​
eventName​

T

fn​

(args) => EventListenerReturnType

Returns​

void

Inherited from​

Emitter.off


on()​

Call Signature​

on<T>(eventName, fn): void

Add an event listener to eventName

Event listeners may optionally return a "defer function" that will be called once all other listeners have been called. This is useful when one listener may want everone to have reacted to an event before calling something.

Type Parameters​
T​

T extends never

Parameters​
eventName​

T

fn​

() => void

Returns​

void

Inherited from​

Emitter.on

Call Signature​

on<T>(eventName, fn): void

Add an event listener to eventName

Event listeners may optionally return a "defer function" that will be called once all other listeners have been called. This is useful when one listener may want everone to have reacted to an event before calling something.

Type Parameters​
T​

T extends keyof PublisherEvents

Parameters​
eventName​

T

fn​

(args) => void

Returns​

void

Inherited from​

Emitter.on


once()​

Call Signature​

once<T>(eventName, fn): void

Add an event listener to eventName that will be called once only

Event listeners may optionally return a "defer function" that will be called once all other listeners have been called. This is useful when one listener may want everone to have reacted to an event before calling something.

Type Parameters​
T​

T extends never

Parameters​
eventName​

T

fn​

() => void

Returns​

void

Inherited from​

Emitter.once

Call Signature​

once<T>(eventName, fn): void

Add an event listener to eventName that will be called once only

Event listeners may optionally return a "defer function" that will be called once all other listeners have been called. This is useful when one listener may want everone to have reacted to an event before calling something.

Type Parameters​
T​

T extends keyof PublisherEvents

Parameters​
eventName​

T

fn​

(args) => void

Returns​

void

Inherited from​

Emitter.once


prepare()​

prepare(): Promise<void>

Start encoding without connecting to the server. Validates tracks and warms up cache.

Returns​

Promise<void>


publish()​

publish(): Promise<void>

Connect to the server and start publishing. Auto-prepares if in "idle" state.

Returns​

Promise<void>

Throws​

if the connection fails


reset()​

reset(): void

Reset the event emitter

Returns​

void

Inherited from​

Emitter.reset


setAuthToken()​

setAuthToken(authToken): void

Set auth token for current and future connections.

Parameters​

authToken​

string

Returns​

void


setChannelId()​

setChannelId(channelId): void

Set channel ID (updates namespace immediately if producer is active).

Parameters​

channelId​

string

Returns​

void


setUrl()​

setUrl(url): void

Set URL for current and future connections.

Parameters​

url​

string

Returns​

void


stats()​

stats(): PublisherStatistics

Get current statistics from the encoding and connection layers.

Returns​

PublisherStatistics


unpublish()​

unpublish(): Promise<void>

Stop the connection but keep encoding active. Call publish again to resume.

Returns​

Promise<void>