Getting Below error message when trying to establish an connection with subscription
ApolloWebSocket.WebSocket.WSError.ErrorType.writeTimeoutError, message: "Timed out waiting for the socket to be ready for a write", code: 0)"
using below code
Using Package 0.53.0 of Apollo
added below Libraries as shown in screen shot
import Apollo
import ApolloWebSocket
class Network {
static let shared = Network()
class NetworkInterceptorProvider: DefaultInterceptorProvider {
override func interceptors<Operation>(for operation: Operation) -> [ApolloInterceptor] where Operation : GraphQLOperation {
var interceptors = super.interceptors(for: operation)
interceptors.insert(AuthorizationInterceptor(), at: 0)
return interceptors
}
}
class AuthorizationInterceptor: ApolloInterceptor {
func interceptAsync<Operation>(
chain: RequestChain,
request: HTTPRequest<Operation>,
response: HTTPResponse<Operation>?,
completion: @escaping (Result<GraphQLResult<Operation.Data>, Error>) -> Void
) where Operation : GraphQLOperation {
// TODO
}
}
private(set) lazy var apollo: ApolloClient = {
let client = URLSessionClient()
let cache = InMemoryNormalizedCache()
let store = ApolloStore(cache: cache)
let provider = NetworkInterceptorProvider(client: client, store: store)
let url = URL(string: "http://apollo-fullstack-tutorial.herokuapp.com/graphql")!
let transport = RequestChainNetworkTransport(interceptorProvider: provider, endpointURL: url)
let webSocket = WebSocket(
url: URL(string: "ws://apollo-fullstack-tutorial.herokuapp.com/graphql")!,
protocol: .graphql_ws
)
let webSocketTransport = WebSocketTransport(websocket: webSocket)
let splitTransport = SplitNetworkTransport(
uploadingNetworkTransport: transport,
webSocketNetworkTransport: webSocketTransport
)
return ApolloClient(networkTransport: splitTransport, store: store)
}()
}
In viewcontroller methods is called in viewDidLoad()
var activeSubscription: Cancellable?
func startSubscription() {
activeSubscription = Network.shared.apollo.subscribe(subscription: TripsBookedSubscription()) { [weak self] result in
guard let self = self else {
return
}
switch result {
case .success(let graphQLResult):
if let tripsBooked = graphQLResult.data?.tripsBooked {
print(tripsBooked)
}
if let errors = graphQLResult.errors {
print(errors)
}
case .failure(let error):
print(error)
}
}
}
Used below URL for testing the subscription
https://apollo-fullstack-tutorial.herokuapp.com/graphql
wss://apollo-fullstack-tutorial.herokuapp.com/graphql
subscription syntex
subscription tripsBooked
{
tripsBooked
}
++ @Priya_Soni