Hi all! I've posted that question on StackOverflow but it hasn't received much attention, hence posting here 🙂
I'm trying to receive financial data from the following WebSockets API: wss://api.qtrade.io/v1/ws
.
I tried it in Swift using the following code in a playground:
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 (see the full program output here).
I also tried connecting to this API in Python with the somewhat equivalent code:
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())
In that case, the response is complete (see the full program output here).
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 (but in Python it's also called only once…) and if yes, how would I fix it?
Thanks in advance for any help! 🙏