WebSockets client on Swift does not receive all message data

Hi all! I've posted that question on StackOverflow but it hasn't received much attention, hence posting here 🙂

I'm trying to receive data from the following WebSockets API: wss://api.qtrade.io/v1/ws.

My code is the following:

Code Block swift
import Foundation
let urlSession = URLSession(configuration: .default)
let url = URL(string: "wss://api.qtrade.io/v1/ws")!
let webSocketTask = urlSession.webSocketTask(with: url)
let message = URLSessionWebSocketTask.Message.string("{\"method\": \"subscribe\", \"channel\": \"market_49\", \"nonce\": \"HXPAxAWn\"}")
webSocketTask.send(message) { error in
if let error = error {
print("WebSocket sending error: \(error)")
}
}
webSocketTask.receive { result in
switch result {
case .failure(let error):
print("Failed to receive message: \(error)")
case .success(let message):
switch message {
case .string(let text):
print("Received text message: \(text)")
case .data(let data):
print("Received binary message: \(data)")
@unknown default:
fatalError()
}
}
}
webSocketTask.resume()


I correctly receive data, i.e. I get in the conditional path of "Received text message", but the response is not complete. I already checked that it's not only the printed output which is truncated, but really the message itself that is not complete.

However, when connecting to this API in Python with the somewhat equivalent code below, everything works fine.

Code Block python
import asyncio
import random
import string
import websockets
async def hello():
uri = "wss://api.qtrade.io/v1/ws"
async with websockets.connect(uri) as websocket:
await websocket.send('{"method": "subscribe", "channel": "market_49", "nonce": "HXPAxAWn"}')
print(f"Sent request")
response = await websocket.recv()
print(f"Received: {response}")
asyncio.get_event_loop().run_until_complete(hello())


What am I missing to get the complete API response with my Swift call? Does it have to do with the fact that the receive closure is called only once because I didn't re-register it (although in Python it's also called only once…) and if yes, how would I fix it?

Thanks in advance for any help! 🙏

When I was working with the webSocket on iOS using only what Apple provides, I found out that the listener always turns off after it receives a message, meaning I always had to turn it on again after each received message. In you particular case, server might just crop the data so it could deliver it faster, and that's why you receive only part of the message. I might be wrong, as my interaction with the webSocket technology is only a few months old and I didn't yet grasped concurrent webSocket connections

WebSockets client on Swift does not receive all message data
 
 
Q