0

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

Mansi
  • 628
  • 1
  • 8
  • 18
  • what do you mean "where message is broadcasted as soon as channel is subscribed, onReceive method is not called there" Where is it broadcasted from? – GorillaApe Mar 08 '18 at 08:22
  • Broadcasted from Rails @GorillaApe, and I used "when", when both the actions occur almost together.. – Mansi Mar 08 '18 at 08:46
  • Well actioncable provides no guarantees or reliability. So messages before subscription are lost. This is a common pubsub issue. A pattern is to get old messages via an api call after subscription is done. If you want to send something during subscription from rails you can call trasmit from there – GorillaApe Mar 08 '18 at 08:52
  • oh I will try that out then, @GorillaApe – Mansi Mar 08 '18 at 08:58

0 Answers0