Following is what my code looks like:
class ADDActionCableHelper {
static let actionCable = ADDActionCableHelper()
private var client: ActionCableClient?
var didReceiveJSON: ADDJSONHandler?
var wrChannel: Channel?
private init() {
guard let url = URL(string: ADDConstants.ActionCable.domain) else {
return
}
client = ActionCableClient(url: url)
}
func connectActionCable() {
guard let client = client else {
return
}
client.connect()
}
func subscribeChannel(forAuthToken token: String, forAppointmentID aID: String) {
guard let client = client else {return}
client.onConnected = { [weak self] _ in
guard let strongSelf = self else {return}
/// Create the Room Channel
let parameters = [
ADDConstants.RequestKeys.actionCableAuthToken: token,
ADDConstants.RequestKeys.actionCableAppointmentId: aID
]
strongSelf.wrChannel = client.create(ADDConstants.ActionCable.wrChannelName, identifier: parameters, autoSubscribe: true, bufferActions: true)
/// Channel callbacks
strongSelf.wrChannel?.onReceive = { [weak self] result, error in
guard let strongSelf = self, let result = result else {return}
strongSelf.didReceiveJSON?(JSON(result))
}
}
}
This is my code for Action Cable. I am using this at multiple places in my project, and everywhere it works perfectly fine except for the one scenario where message is broadcasted as soon as channel is subscribed, onReceive method is not called there, is it something related to synchronisation ?
P.S I am using https://github.com/danielrhodes/Swift-ActionCableClient