danielrhodes / Swift-ActionCableClient

ActionCable is a new WebSocket server being released with Rails 5 which makes it easy to add real-time features to your app. This Swift client makes it dead-simple to connect with that server, abstracting away everything except what you need to get going.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Channel action fired without errors without internet connection

bitomule opened this issue · comments

commented

There's something weird. When I call channel.Action without internet connection it doesn't return any error. isSubscribed seems to be true even without internet connection.

Any idea?

Update: Seems like the client doesn't detect the lost connection. How can I add ActionCableClient manually to the project to test it?

What I do is:

  • Connect to action cable
  • Subscribe to channel
  • Kill internet connection
  • Send an action
  • It's sended without issues, channel.action doesn't fire the expected error

I found out that the onConnect and onDisconnect blocks don't get called.
What I used was subscribing to Starscream notifications:

import Starscream

func addWebSocketsNotifications() {
   NotificationCenter.default.addObserver(self, selector: #selector(webSocketConnected(_:)), name: NSNotification.Name(rawValue: WebsocketDidConnectNotification), object: nil)
        
  NotificationCenter.default.addObserver(self, selector: #selector(webSocketDisconnected(_:)), name: NSNotification.Name(rawValue: WebsocketDidDisconnectNotification), object: nil)
}
    
func webSocketConnected(_ notification: NSNotification) {
   print("\(self.className): web socket connected")
}
    
func webSocketDisconnected(_ notification: NSNotification) {
    print("\(self.className): web socket disconnected")
}

You can try using this. Although I get an error when trying to send a message without a connection.

@bitomule The client will take the message and keep retrying the connection and send the message if there is a successful reconnection. You can change this behavior.

commented

@danielrhodes how do I change that? I already handle trying again when connection is restored.

when you create a channel:

client.create("ChannelName", identifier: nil, autoSubscribe: true, bufferActions: false)

That will then mean that if it can't send an action immediately it should throw an error. However, be warned that ActionCable does not acknowledge actions, so there is no guarantee the server received it even though the connection may appear as alive on the websocket level.

commented

I already have buffer actions disabled but still doesn't get the error. Is there any way I can ping ws before sending an action to be sure actions are only sended when connection is working?

commented

Ok, the issues seems to be I was testing in the simulator. Testing in device seems to report connected an disconnected as expected.

@bitomule That's odd. I don't think there should be any substantial difference between the two.

commented

@danielrhodes I think the issue was killing macbook wifi connection instead of plane mode inside the simulator.