Using the Realtime API you can create channels and channel resources where messages can be published. Site visitors who subscribe to a channel receive the messages published to that channel. When a message is received you can handle the message any way you choose, such as displaying the contents of the message or changing the state of page elements depending on the contents of the message. You can also manage the permissions of who can read from specific channels.
The wix-realtime-frontend
module is used in conjunction with the
wix-realtime-backend
module.
Get hands-on experience with the Realtime API on our Hello Realtime example page.
Typical uses of the Realtime API include implementing a messaging system or as an alternative to using a polling mechanism.
The Realtime API uses the following terminology:
For a tutorial on using the Realtime API, see Sending Messages with the Realtime API.
To use the Realtime API, import wixRealtimeFrontend
from the wix-realtime-frontend
module:
import wixRealtimeFrontend from "wix-realtime-frontend";
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.
function onConnected(handler: function): void;
handler(): void
The name of the function or
the function expression to run when a connection occurs.
import wixRealtimeFrontend from "wix-realtime-frontend";
// ...
wixRealtimeFrontend.onConnected(() => {
// handle connection
$w("#disconnectedMessage").hide();
});
Adds an event handler that runs when a disconnection occurs.
A disconnection can occur, for example, if there is a network issue.
function onDisconnected(handler: function): void;
handler(): void
The name of the function or
the function expression to run when a disconnection occurs.
import wixRealtimeFrontend from "wix-realtime-frontend";
// ...
wixRealtimeFrontend.onDisconnected(() => {
// handle disconnection
$w("#disconnectedMessage").show();
});
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()
function to reject
and do not trigger onError()
event handlers.
function onError(handler: function): void;
handler(error: Error): void
The name of the function or
the function expression to run when an error occurs.
import wixRealtimeFrontend from "wix-realtime-frontend";
// ...
wixRealtimeFrontend.onError((error) => {
let code = error.errorCode;
let message = error.message;
if (error.channel) {
let channelName = error.channel.name;
let resourceId = error.channel.resourceId;
}
});