mir
mir

Reputation: 183

Integrate Apollo subscriptions on iOS with Action Cable being used on the backend for websockets

I'm trying to make Apollo subscriptions on iOS work with a backend that is using Action Cable to implement websockets. I learned that the iOS app needs to send command, channel and channel id to the backend to make subscriptions work (see here). I have tried to use the function write func write(_ str: String, force forced: Bool = false, id: Int? = nil) in WebSocketTransport.swift on WebSocketTransport object when initializing instance of Apollo. Below you can see how I'm doing that.

        let userDefault = UserDefaults.standard
        var authPayloads = Dictionary<String, String> ()
        var authToken = ""
        if let token = userDefault.object(forKey: "token") {
            authToken = "\(token)"
            authPayloads.updateValue(authToken, forKey: "authorization")
        }
       let configuration = URLSessionConfiguration.default
       configuration.httpAdditionalHeaders = authPayloads

        let map: GraphQLMap = authPayloads
        let wsEndpointURL = URL(string: "ws://localhost:8080/subscriptions/\(authToken)")!
        let endpointURL = URL(string: "http://localhost:8080/api")!

        websocket = WebSocketTransport(request: URLRequest(url: wsEndpointURL), connectingPayload: map)
       var channelId = Int(arc4random_uniform(100000))
       websocket?.write(stringify(json: ["command":"subscribe", "identifier": stringify(json: ["channel":"channelName", "channelId": channelId])]))

        let splitNetworkTransport = SplitNetworkTransport(
            httpNetworkTransport: HTTPNetworkTransport(
                url: endpointURL,
                configuration: configuration
            ),
            webSocketNetworkTransport: websocket!
        )
        return ApolloClient(networkTransport: splitNetworkTransport)
    }()

However, the backend isn't seeing what I'm writing to the WebSocket Transport object in their logs and I'm not able to subscribe to that specific channel. Any idea how I can make use Apollo subscriptions on iOS if the backend is using Action Cable, and make the two work together?

Upvotes: 4

Views: 816

Answers (1)

Mohamad Kaakati
Mohamad Kaakati

Reputation: 388

The only solution for now is to use Swift-ActionCableClient to recieve streams, and use the mutations/queries from ApolloClient.

Unfortenatly! Apollo iOS doesn't know how to communicate with ActionCable channels, this issue is reported on Apollo-iOS Github Issue #634 & Your issue Github Issue #454

Upvotes: 0

Related Questions