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

Pub Sub System

Roman Baitaliuk edited this page Jan 20, 2018 · 5 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.

/**
    channel name: string - can be any string you wish
*/
let channel = webSocket.subscribe(channelName: "channel name")

/**
    data: any - is what you get when you or some one else publish to the channel
*/
channel.watch { (data) in
    // in here you can write any logic
}

/**
    data: any - is what you want to publish to the channel (everyone who is subscribe will get it)
*/
channel.publish(data: "some data")

/**
    This method is used to unsubscribe from the channel
*/
channel.unsubscribe()

also you can chain everything in one expression

let channel = webSocket.subscribe(channelName: "channel").watch { (data) in
    // in here you can write any logic
}.publish(data: "some data")

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

webSocket.getChannel(byName: 'channel name')

/**
    You can get an array of all subscribed channels
*/
let channels = webSocket.getChannels()

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

Clone this wiki locally