Description:
I am implementing a Socket.IO connection in an iOS app utilizing socket.io-client-swift. The backend makes use of authentication through headers (user-id and sessionkey). The difficulty I am dealing with is:
The consumer seems to attach efficiently, even with invalid headers.
Nevertheless, the server by no means receives the emitted occasions, nor does it reply with any knowledge.
On Android/Postman, the socket connects however disconnects instantly if the auth headers are invalid — which is predicted and proper.
On iOS, the socket stays linked even with fallacious headers, however behaves like a “phantom connection” — no occasion supply, no server response.
SocketManager Code (Be aware: This was generated by ChatGPT, I had one which I made however after 2 days of debugging and repetitions that is what I’ve landed on)
import SocketIO
ultimate class SocketManager {
static let shared = SocketManager()
personal var supervisor: SocketIO.SocketManager?
personal var socket: SocketIOClient?
personal init() {}
func join(
userId: String,
sessionKey: String,
onMessageReceived: @escaping ([String: Any]) -> Void,
onSenderMessage: @escaping ([String: Any]) -> Void
) {
guard let url = URL(string: "https://instance.com/socket.io") else {
print("Invalid URL")
return
}
let config: SocketIOClientConfiguration = [
.log(true),
.compress,
.extraHeaders([
"user-id": "(userId)",
"sessionkey": sessionKey
]),
.forceNew(true),
.reconnects(true)
]
self.supervisor = SocketIO.SocketManager(socketURL: url, config: config)
self.socket = self.supervisor?.defaultSocket
guard let socket = self.socket else { return }
socket.on(clientEvent: .join) { knowledge, ack in
print("Socket linked")
}
socket.on("receiver-message") { knowledge, _ in
guard let json = knowledge.first as? [String: Any] else { return }
onMessageReceived(json)
}
socket.on("sender-message") { knowledge, _ in
guard let json = knowledge.first as? [String: Any] else { return }
onSenderMessage(json)
}
socket.on(clientEvent: .disconnect) { _, _ in
print("Socket disconnected")
}
socket.on(clientEvent: .error) { knowledge, _ in
print("Socket error", knowledge)
}
socket.join()
}
func sendMessage(messageData: [String: Any]) {
guard socket?.standing == .linked else {
print("Socket not linked. Message not despatched.")
return
}
socket?.emit("sendMessage", messageData)
}
func disconnect() {
socket?.disconnect()
}
}
I’ve tried a number of configurations and approaches for final 2 days. I am not ready to determine if its concern with iOS aspect or server aspect. Is there something I have to set on iOS aspect in plist? I’ve ATS added with arbitrary load allowed already.