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

Action Cable on receive method is not called

MandeepSingh1 opened this issue · comments

Hello Daniel am using the action cable, I will explain you the scenario for the onReceive method not called.

When we start the application then the action cable is working fine, But if I logout from my application and then login from different user then the action cable of onReceive method is not called. I don't understand what happens but if I kill the application and then start it again. Then the action cables onReceive method will work. I don't know what am doing wrong.

Can you suggest me what I have to do.

@danielrhodes onReceive method is not called when message is broadcasted as soon as channel is subscribed..

Hi @mansi-cherry , can you explain me your scenario how you are using the action cables. May be I can help you. I have resolved my issue.

@MandeepSingh1 How did you solve this?

I have used action cable in multiple cases in my app, but for one time, message from the rails is broadcasted as soon as my channel is subscribed. So for that case I am not getting onReceive callback. Everywhere else it is working fine!

In my case when ever am login in my app and then I call the cables to connect. But when I press the logout, then I disconnect the action cables. Whenever I open the application again I do connect the action cables again.

No, I have no such issue related to connecting and disconnecting.

@mansi-cherry , can you show me your code may be I can help you in code rather then discussing the point in comments. Have you pass the same function name and channel name in your code?

private var client: ActionCableClient?
var didReceiveJSON: ADDJSONHandler?
func subscribeChannel(forAuthToken token: String, 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 = [
                "actionCableAuthToken": token,
                "Id": 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))
            }
        }
    }

I appreciate your coding standard is very good. Please call this methods also and check whether your method is getting connected or not, you have to replace the roomFollowUser with client. Check whether your channel is subscribing properly or not.

// A channel has successfully been subscribed to.
roomFollowUser.onSubscribed = {
print("successfully followParticularUser subscribed")
}

    // A channel was unsubscribed, either manually or from a client disconnect.
    roomFollowUser.onUnsubscribed = {
        print("Unsubscribed")
    }
    
    // The attempt at subscribing to a channel was rejected by the server.
    roomFollowUser.onRejected = {
        print("Rejected")
    }

Yes I checked that, by printing the onSubscribed CallBacks

Basically when am facing this issue. When I logout from my application and then try to login from different user then the action cable of onReceive method is not getting called thats my scenario and rails methods is working fine. Rest of your functions are getting called when you call the action cables method or not?

yeah mine is not that kind of scenario, but the issue is same "onReceive not called"

When you are trying to subscribe the channel in viewDidLoad, viewWillAppear? Or May be you are trying to subscribe the multiple channels simultaneously?

I subscribe in viewWillAppear

Do you have multiple channels to subscribe in viewWillAppear or just a single channel?

Single channel only

Try to subscribe the channel in

override func viewDidAppear(_ animated: Bool) {
self.addActionCableSubscribeChannels()
}

override func viewDidDisappear(_ animated: Bool) {
    ActionCableClientSingleton.sharedInstance.roomFollowUser.unsubscribe()
}

Hey @danielrhodes I am using Rails 5 version.
Can it be the reason that broadcast happens as soon as subscribed the channel, and hence onReceive is not called, may be async operations don't work?

Hey @mansi-cherry - we are having the same issues, did you find a resolution?

@johnmcauley Actually in my case, I was maintaining the action cable instance as a Singleton instance. So it was not handled properly from my side
Another thing is that there are 2 ways to initialise action cable client, for me one of them didn't seem to work so I used the another way. Had I have the access of source code now, I would have shared the code snippet.