/** * An object representing a channel or channel resource. * Site visitors can subscribe to a channel or channel resource to receive the * messages that are published on it. When site visitors subscribe to a channel, they do not * receive messages published to a resource on that same channel. Similarly, * when site visitors subscribe to a channel resource, they do not * receive messages published to that same channel without a specified resource. * Channel resources inherit their parent channel's permissions, unless specified * otherwise. */ interface Channel { /** * Channel name. Cannot exceed 140 characters. * @requiredField name */ name: string; /** * ID of a specific channel resource. */ resourceId?: string; } /** * An object representing an error. */ interface Error { /** * Error code. * * One of: * * + `1`: Connection error * + `2`: Subscription error (error will also include a channel value) * + `3`: Bad input * @requiredField errorCode */ errorCode: number; /** * Error message. * @requiredField message */ message: string; /** * Channel related to the error. * @servicePath wix-realtime-frontend.Channel */ channel?: Channel; } /** * An object representing a published message. */ interface Message { /** * Message payload. * Max: 10kb * @requiredField payload */ payload: any; /** * Message publisher, if sent. * @servicePath wix-realtime-frontend.Publisher */ publisher?: Publisher; } /** * An object representing a publisher. */ interface Publisher { /** * ID of the publisher. * @requiredField id */ id: string; } /** * An object containing options to be used when unsubscribing from a * channel or channel resource. */ interface UnsubscribeOptions { /** * The channel, and optionally the resource, to * unsubscribe from. Use this property when unsubscribing from all subscriptions to a channel * or channel resource. * @servicePath wix-realtime-frontend.Channel */ channel?: Channel; /** * ID of the subscription to unsubscribe from. Use this * property when unsubscribing from one of multiple subscriptions to a channel or channel * resource. */ subscriptionId?: string; } /** * undefined * * Handles connections. */ type ConnectionHandler = () => void; /** * undefined * * Handles disconnections. */ type DisconnectionHandler = () => void; /** * undefined * * Handles realtime errors. * @param error - The error that occurred. * @requiredField error * @servicePath wix-realtime-frontend.Error */ type ErrorHandler = (error: Error) => void; /** * undefined * * Handles a message published to a channel or channel resource. * @param message - The message that was published. * @requiredField message * @servicePath wix-realtime-frontend.Message * @param channel - The channel the message was published on. * @requiredField channel * @servicePath wix-realtime-frontend.Channel */ type MessageHandler = (message: Message, channel: Channel) => void; /** * Adds an event handler that runs when a connection or reconnection * occurs. * * A connection occurs on the first subscription to a channel or channel resource or * when an existing connection is lost and then restored. * @param handler - The name of the function or * the function expression to run when a connection occurs. * @requiredField handler * @servicePath wix-realtime-frontend.ConnectionHandler */ declare function onConnected(handler: ConnectionHandler): Promise; /** * Adds an event handler that runs when a disconnection occurs. * * A disconnection can occur, for example, if there is a network issue. * @param handler - The name of the function or * the function expression to run when a disconnection occurs. * @requiredField handler * @servicePath wix-realtime-frontend.DisconnectionHandler */ declare function onDisconnected(handler: DisconnectionHandler): Promise; /** * Adds an event handler that runs when an error occurs. * * An error can occur when attempting to resubscribe after a disconnection. * Errors that occur during the initial subscription process cause the * Promise returned by the [`subscribe()`](#subscribe) function to reject * and do not trigger `onError()` event handlers. * @param handler - The name of the function or * the function expression to run when an error occurs. * @requiredField handler * @servicePath wix-realtime-frontend.ErrorHandler */ declare function onError(handler: ErrorHandler): Promise; /** * Subscribes to a channel or channel resource. * * The `subscribe()` function subscribes to receive messages published on the * specified channel or channel resource and sets a function that handles * incoming messages when they are published to the channel. * * When you subscribe to a channel you do not receive messages published to * a resource on that same channel. To receive those messages, you must * explicitly subscribe to the specific resource. * * The specified message handler is run each time a message is published on * the given channel or channel resource. Use it to define what happens when * messages are received. * * You can subscribe to a channel or channel resource more than once to add * multiple message handlers. Each subscription returns a unique subscription * ID which can be used later to unsubscribe from the specific subscription * with that ID, without affecting the other subscriptions on the same channel * or channel resource. * @param channel - The channel, and optionally the resource, to * subscribe to. * @requiredField channel * @servicePath wix-realtime-frontend.Channel * @param handler - The name of the function or * the function expression to run when something is published to the subscription's * channel or channel resource. * @requiredField handler * @servicePath wix-realtime-frontend.MessageHandler * @returns Fulfilled - An ID representing the subscription, used for unsubscribing. * Rejected - Error that caused the rejection, such as insufficient * permissions. */ declare function subscribe(channel: Channel, handler: MessageHandler): Promise; /** * Unsubscribes from a channel or channel resource. * * The `unsubscribe()` function unsubscribes from receiving messages published * on the specified channel or channel resource. * * + Use the `channel` property of the `UnsubscribeOptions` object * passed to the `options` parameter to unsubscribe from all subscriptions to a * specific channel or channel resource. * * When you unsubscribe from a channel you do not unsubscribe from the * resources on that same channel. To no longer receive messages on a * channel resource, you must explicitly unsubscribe from the specific resource. * * + Use the `subscriptionId` property of the `UnsubscribeOptions` object * passed to the `options` parameter to unsubscribe from a specific subscription. * * This option is typically used when you have more than one subscription to the same * channel or channel resource and you only want to unsubscribe from one of those subscriptions. * * Get a subscription's `subscriptionId` from the resolution of the Promise returned * when creating the subscription with the [`subscribe()`](#subscribe) function. * @param options - Options to use when unsubscribing. * @requiredField options * @servicePath wix-realtime-frontend.UnsubscribeOptions * @returns Fulfilled - When unsubscribed from the channel or channel resource. * Rejected - Error that caused the rejection. */ declare function unsubscribe(options: UnsubscribeOptions): Promise; declare const realtimeSdkModuleContext_onConnected: typeof onConnected; declare const realtimeSdkModuleContext_onDisconnected: typeof onDisconnected; declare const realtimeSdkModuleContext_onError: typeof onError; declare const realtimeSdkModuleContext_subscribe: typeof subscribe; declare const realtimeSdkModuleContext_unsubscribe: typeof unsubscribe; declare namespace realtimeSdkModuleContext { export { realtimeSdkModuleContext_onConnected as onConnected, realtimeSdkModuleContext_onDisconnected as onDisconnected, realtimeSdkModuleContext_onError as onError, realtimeSdkModuleContext_subscribe as subscribe, realtimeSdkModuleContext_unsubscribe as unsubscribe }; } type Methods = { [P in keyof T as T[P] extends Function ? P : never]: T[P]; }; declare const realtimeContext: Methods; export { realtimeContext as realtime };