Wix.PubSub

publish

Broadcasts an event to other Site components of a multicomponent app. If the app’s Site components span multiple pages, they will be notified when they are rendered.

SDK Version: SDK 1.25.0+
Editor Version: New Editor, Old Editor
Display: Live Site, Preview
Components: Widget, Pinned (aka Fixed-Position) Widget, Page, Modal, Popup

Note:
For the Worker component, use the Worker.PubSub.publish method instead.

Syntax:

Copy
1
publish(eventName, data, isPersistent)

Parameters:

NameTypeDescription
eventName (required)StringThe name of the event to publish
data (required)ObjectThe object to send to subscribers for this event type
isPersistent (required)BooleanIndicates whether this event is persisted for event subscribers who have not yet subscribed

Example:

Copy
1
// The following call publishes an app event.
2
// Apps components that are subscribed to this event will receive it when they are rendered.
3
Wix.PubSub.publish("my_event_name", {value: "this is my message"}, true);

subscribe

Subscribes to events from other site components of a multicomponent app. If the components span multiple pages, they will be notified once they are rendered. It is also possible to receive all notifications prior to rendering by specifying a flag when subscribing to events. If the flag is set, the component will be notified immediately of any prior events of the type it is registered to receive.

SDK Version: SDK 1.25.0+
Editor Version: New Editor, Old Editor
Display: Live Site, Preview
Components: Widget, Pinned (aka Fixed-Position) Widget, Page, Modal, Popup

Note:
For the Worker component, use the Worker.PubSub.subscribe method instead.

Syntax:

Copy
1
subscribe(eventName, callBack, \[receivePastEvents\])

Parameters:

NameTypeDescription
eventName (required)String The name of the event to subscribe to
callBack (required)FunctionFunction that will respond to events sent from other components of the broadcasting app. It will be given the event object itself and the source of the event
receivePastEventsBooleanA flag to indicate that all past instances of the registered event should be sent to registered listener. This will happen immediately upon registration.

Example:

Copy
1
//subscribe and then unsubscribe to "my_event_name" event
2
Wix.PubSub.subscribe("my_event_name", function(event) {
3
// process the event which has the following format:
4
// {
5
// name: eventName,
6
//     data: eventData,
7
//     origin: compId
8
// }
9
});
10
11
// subscribe to "my_event_name" event, events which also happened before this component was rendered will send
12
Wix.PubSub.subscribe("my_event_name", function(event) { }, true);

unsubscribe

Unsubscribes from receiving further events. The id from the initial subscribe call is used to unsubscribe from further notifications.

SDK Version: SDK 1.25.0+
Editor Version: New Editor, Old Editor
Display: Live Site, Preview
Components: Widget, Pinned (aka Fixed-Position) Widget, Page, Modal, Popup

Syntax:

Copy
1
unsubscribe(eventName, function)

Parameters:

NameTypeDescription
eventName (required)String The name of the event to unsubscribe from
function (required)Function The function that will respond to events sent from other extensions of the broadcasting app. It will be given the event object itself and the source of the event.

Example:

Copy
1
//subscribe and then unsubscribe to "my_event_name" event
2
var id = Wix.PubSub.subscribe("my_event_name", function(event) { });
3
Wix.PubSub.unsubscribe("my_event_name", id);
Was this helpful?
Yes
No