Channel API Reference

The Channel object represents a real-time communication channel in the Ember Link platform. It provides functionalities for presence tracking, storage synchronization, managing users, and interacting with WebSocket connections.


Connecting to a Channel

To create a new instance of a Channel, you need to call the joinChannel function on a Client

ChannelConfig

interface ChannelConfig<
	S extends IStorageProvider,
	P extends Record<string, unknown> = DefaultPresence
> {
	channelName: string;
	baseUrl: string;
	authenticate: () => Promise<AuthValue>;
	createWebSocket: (authValue: AuthValue) => WebSocket;
	options?: {
		initialPresence?: P;
		storageProvider?: S;
		autoConnect?: boolean;
	};
}
NameTypeRequiredDescription
optionsObjectOptional configuration for the channel.
└ initialPresenceType of PresenceOptional initial presence state for the channel.
└ storageProviderSOptional storage provider for syncing data.
└ autoConnectbooleanWhether to automatically connect to the channel (default: true).

Methods

type Channel<
	P extends Record<string, unknown> = DefaultPresence,
	C extends Record<string, unknown> = DefaultCustomMessageData
> = {
	updatePresence: (state: P) => void;
	hasStorage: () => boolean;
	getStorage: () => IStorage;
	getStatus: () => Status;
	getOthers: () => User<P>[];
	getName: () => string;
	getPresence: () => P | null;
	destroy: () => void;
	updateYDoc: (data: StorageUpdateMessage) => void;
	syncYDoc: (data: StorageSyncMessage) => void;
	events: Observable<ChannelEvents<P, C>> & {
		others: Observable<OtherEvents<P>>;
		yjsProvider: Observable<YjsProviderEvents>;
	};
};
NameTypeDescription
updatePresence(state: P) => voidUpdates the local users presence in the channel.
hasStorage() => booleanReturns true if the channel has a storage provider, false otherwise.
getStorage() => IStorageRetrieves the channel’s storage provider.
getStatus() => StatusReturns the current connection status of the channel.
getOthers() => User<P>[]Retrieves the list of other users in the channel.
getName() => stringReturns the name of the channel.
getPresence() => PReturns the presence state of the channel or null if unavailable.
destroy() => voidDestroys the channel, cleaning up resources and connections.
updateYDoc(data: StorageUpdateMessage) => voidSends an update to the Yjs document in the channel.
syncYDoc(data: StorageSyncMessage) => voidSynchronizes the Yjs document in the channel.
eventsObservable<ChannelEvents<P, C>>Event emitter for channel-specific events. Includes others and yjsProvider.

ChannelEvents

type ChannelEvents<
	P extends Record<string, unknown> = DefaultPresence,
	C extends Record<string, unknown> = DefaultCustomMessageData
> = {
	presence: (self: P) => void;
	status: (status: Status) => void;
	others: (others: User<P>[]) => void;
	destroy: () => void;
	customMessage: (message: Extract<ServerMessage<P, C>, { type: 'custom' }>['data']) => void;
};
Event NameTypeDescription
presence(self: P) => voidEmitted when the presence state of the local user changes.
status(status: Status) => voidEmitted when the connection status of the channel changes.
others(others: User<P>[]) => voidEmitted when the list of users in the channel changes.
destroy() => voidEmitted when the channel is destroyed.
customMessage(message: C) => voidEmitted when a custom message is received in the channel.

Example Usage

import { Client } from '@ember-link/core';

const client = new Client({
	baseUrl: 'https://localhost:9000',
	multiTenant: { tenantId: 'team-123' }
});

const { channel, leave } = await client.joinChannel('editor-room', {
	initialPresence: { username: 'Julia' }
});

channel.events.subscribe('other', (others) => {
	console.log('Users online:', others);
});

channel.updatePresence({ isTyping: true });
// Call leave when you are done with the channel
leave();

Notes

  • The updatePresence method allows you to update your own presence state (such as your username or status).

  • The getStorage and hasStorage methods help interact with a configured storage provider for syncing data.

  • The events property provides observables for listening to channel events, including presence, status changes, and Yjs document updates.