Swift NIO Secured Websockets?

I am trying to run a websocket server and client in the same app. Will the View starting the server and client is webpage loaded throught webview (HTTPS) to connect to wss://


I adopted the samples from swift-nio-transport services and i could get the scenrio working BUT I cannot get the secured wss over https working.


Is this possible? This discussion seems to defer (https://forums.developer.apple.com/thread/104376)


To provide some context - I am did this - https://stackoverflow.com/questions/57465854/swift-nio-secured-websocket-server

Replies

Before we start, two things:

  • I don’t think anyone with deep SwiftNIO expertise lurks here on DevForums. You'd be better off asking on Swift Forums > Related Project > SwiftNIO.

  • iOS 13 and friends support a native WebSocket API, including a high-level client based on

    URLSession
    and a low-level client and server in the Network framework. See WWDC 2019 Session 712 Advances in Networking, Part 1 for the detail.

Coming back to your big picture, you wrote:

I am trying to run a websocket server and client in the same app.

I recommend against doing this. There’s at least two problems you will encounter:

  • WebSocket requests, being network requests, can fall foul of the web view’s same-origin policy.

  • Loopback requests like this can run into problem when deployed in a per-app VPN environment.

A better option for web-to-native communication is

WKScriptMessage
.

If you have a bunch of existing web code that assumes WebSocket semantics, it’s feasible to build a shim that looks like a WebSocket but actually operates in terms of

WKScriptMessage
.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"