Pub / Sub app
Code a Pub/Sub app to publish ‘greetings ’ message. The app will also receive the greetings message from other app instances. We can run 100's of these pub/sub app instances. Each of them will send a single greeting message and will receive greetings from all the other app instances.
Before trying this sample, follow the Setup and initialization . For more information, see the Channels Pub/Sub API reference documentation.
Download source
Download the pub_sub_app by clicking on this link
Dependencies
To add databridges-io/lib.ios.sio.client package dependency to your XCode project, select File > Swift Packages > Add Package Dependency and enter repository URLhttps://github.com/databridges-io/lib.ios.sio.client.git
. You can also navigate to your target’s General pane, and in the “Frameworks, Libraries, and Embedded Content” section, click the + button, select Add Other, and choose Add Package Dependency.
In XCode's Project navigator, the Swift Package Dependencies section shows the newly added package dependency. Click the disclosure triangle to view the contents of the package as it exists locally on your Mac.
Create a class inside Shared
folder and name it as pub_sub_app
, this will create pub_sub_app.swift
file.
Copy paste below code and you are ready to run this example.
/*
Code a Pub/Sub app to publish 'greetings ' message. The app will also receive the greetings message from other app instances.
We can run 100's of these pub/sub app instances. Each of them will send a single greeting message and will receive greetings from all the other app instances.
*/
import Foundation
// Include dataBridges client library package
import databridges_sio_swift_client
public class pubsubapp{
// Initialize dataBridges client
public var dbridge:databridges_sio_swift_client = databridges_sio_swift_client()
public var helloWorld:Subscribe?=nil;
public var sessionID:String
public func randomString(of length: Int) -> String {
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var s = ""
for _ in 0 ..< length {
s.append(letters.randomElement()!)
}
return s
}
public func Execute() {
// Replace your application key below, which you have received from dataBridges management portal.
dbridge.appkey = "____appKey______";
// Replace your authentication url below, which you have received from dataBridges management portal.
// example https://endpoint01.databridges.io/client/v1/authenticate
dbridge.auth_url = "_____URL_____";
// lets create a sessionID
self.sessionID = self.randomString(7);
print("sessionID is \(self.sessionID)")
try? self.dbridge.connectionstate?.bind("connected", { (event: Any) in
print("Connected to dataBridges real-time network !!!")
do{
// Subscribe to channel 'Hello-World'
try self.helloWorld = (self.dbridge.channel?.subscribe("Hello-World"))!
}catch {
let db = error as? dBError
print("Channel subscription faced an exception: \(db?.source) \(db?.code) \(db?.message)")
}
if (self.helloWorld != nil){
// We will bind to subscription success event. On success, we will publish event messages.
try? self.helloWorld?.bind("dbridges:subscribe.success", { (event: Any , metadata: MetaData) in
print("dbridges:subscribe.success \(metadata.ToString())")
// we can also check if the helloWorld subscription isOnline.
if(self.helloWorld.isOnline() != nil){
print("Publishing greetings ")
do{
// Subscribe to channel 'Hello-World'
try self.helloWorld?.publish("greetings", "Greetings from " + self.sessionID)
}catch {
let db = error as? dBError
print("Publishing messages faced an exception: \(db?.source) \(db?.code) \(db?.message)")
}
}
});
//Bind to the channel.event that you want to process. Here we will bind to 'greetings'
try? self.helloWorld?.bind("greetings", { (payload: Any , metadata: MetaData) in
print("received greeting message : \(payload as! String)")
});
// Bind to Subscription failure event, to get notified if subscription to channel has been rejected by dataBridges network
try? self.helloWorld?.bind("dbridges:subscribe.fail", { (event: Any , metadata: MetaData) in
let db = event as? dBError
print("Channel subscription faced an exception: \(db?.source) \(db?.code) \(db?.message)");
});
}
});
//Bind to disconnected event, to get intimation about dataBridges network disconnection.
try? self.dbridge.connectionstate?.bind("disconnected", { (event: Any) in
print("Disconnected from dataBridges real-time network")
})
// Connect to dataBridges. If any runtime error it will be caught in catch().
do{
try self.dbridge.connect()
}catch {
let db = error as? dBError
print("dataBridges Connection exception.. \(db?.source) \(db?.code) \(db?.message)")
}
}
}
Save your file and build your project in Release or Debug mode to test.