Apple private relay and Websocket messaging

Hi.

i've app in swift for iOS and macOS, which using websocket

webSocketTask = URLSession.shared.webSocketTask(with: request)
        
        //Session
        let session = URLSession(configuration: configuration ?? .default, delegate: self, delegateQueue: OperationQueue())
       

and as the result, when user have turn on Private relay, user doesn't receive any informatiom from web socket, if user switch off this function, all is fine. Could you give some advise, how to setup client, or server to allow working websockets in that case?

Answered by DTS Engineer in 762226022

I keep meaning to come back to this but I never have time )-: At this point I just have to admit that this is not going to happen in the context of DevForums. I recommend that you open a DTS tech support incident so that I can allocate more time to your issue.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

iCloud Private Relay should be compatible with WebSocket without you having to jump through any hoops. As a first step I’d like to rule out any URLSession involvement. Are you able to reproduce this when you use Network framework directly?

For a simple example of how to configure an NWConnection with the WebSocket protocol, see this post.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks for response. For some reasons i've write my own code for clients. class newWebsocket{ private var connection: NWConnection?

private var additionalHeaders: [(String, String)]

private let delegate : ServersListUpdateDelegate
    
init(delegate : ServersListUpdateDelegate){
    self.delegate = delegate
    
    if(Constants.shared.isStageEnvironment && AuthenticationManager.authToken != nil){
        print("Authvalue is : \(AuthenticationManager.authToken!)")
        additionalHeaders = [
            ("Authorization", AuthenticationManager.authToken!)
        ]
    }
    else{
        additionalHeaders = [
            ("key", Constants.shared.authKey)
        ]
    }
    
}

 func connect() {
    
    let url = URL(string: Constants.shared.socketUrl)!
    let endpoint = NWEndpoint.url(url)

 
     let params = NWParameters.tcp
     let stack = params.defaultProtocolStack
     let ws = NWProtocolWebSocket.Options(.version13)
     stack.applicationProtocols.insert(ws, at: 0)

    // Create the WebSocket connection

    connection = NWConnection(to: endpoint, using: params)

    // Set up the state change handler
    connection?.stateUpdateHandler = { [weak self] newState in
        switch newState {
        case .ready:
            print("🚧WebSocket connection established.")
            // Start receiving messages when the connection is ready
            self?.receiveMessage()
        case .waiting(let error):
            print("🚧WebSocket connection is waiting: \(error)")
        case .failed(let error):
            print("🚧WebSocket connection failed: \(error)")
        case .cancelled:
            print("🚧WebSocket connection cancelled")
        default:
            break
        }
    }
    
    // Start the connection
    connection?.start(queue: .main)
}

private func receiveMessage() {
    connection?.receiveMessage { (data, context, isComplete, error) in
        if let error = error {
            print("Failed to receive message: \(error)")
            return
        }
        
        if let data = data, let message = String(data: data, encoding: .utf8) {
            print("Received message: \(message)")
            // Process the received message as needed
        }
        
        // Continue to receive messages recursively
        self.receiveMessage()
    }
}

func reset(){}

func disconnect() {
    connection?.cancel()
}

}

And i've get in any case error 🚧WebSocket connection is waiting: POSIXErrorCode(rawValue: 53): Software caused connection abort also i've trying to remove from server side, and client side Auth headers, but it not helped

Also i've tried library from github NWWebSocket i've get the same error Darwin.POSIXErrorCode ECONNABORTED

Well that at least explains why URLSession is working.

As to what’s going on here, it’s hard to say. Can you post the WebSocket URL you’re trying to connect to?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Can't post it here, but send it to your email

@eskimo if you able, please give some advise, WDYT?

Accepted Answer

I keep meaning to come back to this but I never have time )-: At this point I just have to admit that this is not going to happen in the context of DevForums. I recommend that you open a DTS tech support incident so that I can allocate more time to your issue.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Apple private relay and Websocket messaging
 
 
Q