Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Pub Sub System

Dmitrii Goriunov edited this page May 14, 2018 · 8 revisions

ClusterWS Pub/Sub system is the only way to communicate between different clients client <-> client communication. In this documentation we will show the only functionality of Pub/Sub system for more examples of client <-> client communication check Client to Client Communication guide.

...
let channel

socket.on('connect', () => {
    // Subscribe to the channel (can be any channel you want)
    channel = socket.subscribe('channel-name')
    
    // Listen on messages to that channel
    channel.watch((message) => {
        // Execute code when receiving messages on the channel 
    })

    // Publish any message you want
    channel.publish('any-data-you-want')

    // Unsubscribe from the channel
    channel.unsubscribe()
})
...

also you can chain everything if you want

...
socket.on('connect', () => {
    socket.subscribe('channel-name').watch((message) => {
        // Execute code when receiving messages on the channel
    }).publish('any-data-you-want')
})
...

in case if you want to get the channel from the socket (must be subscribed before) you can use getChannelByName method

...
const channel = socket.getChannelByName('channel-name')
channel.publish('data-to-that-channel')
...

Note: You can subscribe to the channels only if the socket is connected to the server (not before that). To make sure that socket is subscribed you can do it on connect event or on any other events you emit from the server.

English:

1. Home

2. Installation and Configuration

3. Handle Socket

4. Pub/Sub System

5. Client to Client Communication

Other languages will be added later

Clone this wiki locally