WebSocket connection fails in WatchOS9

Socket connection doesn't work on real devices for watchOS 9.

It was working well on WatchOS 7 & WatchOS 8.

Is this a bug or a new restriction for WatchOS 9 ?

Thank you.

We've seen the same thing. I've put together a small repro here that includes the Xcode console message: https://gist.github.com/ryanwilson/fd5454a681eab32348ebc7119949182c

This comes down to two errors. One is logged:

2022-09-16 09:41:54.432645-0400 watchOS9Websockets Watch App[968:637613] Error getting network data status Error Domain=NSPOSIXErrorDomain Code=19 "Operation not supported by device"

The other is returned as an error when trying to use the send function on URLSessionWebSocketTask:

2022-09-16 09:41:54.445194-0400 watchOS9Websockets Watch App[968:637619] Task <7AA472C9-8847-4D10-B15D-EA7C970BE6F0>.<1> finished with error [-1009] Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={NSErrorFailingURLStringKey=https://ws.postman-echo.com/raw, NSErrorFailingURLKey=https://ws.postman-echo.com/raw, NSLocalizedDescription=The Internet connection appears to be offline., _NSURLErrorRelatedURLSessionTaskErrorKey=(    "LocalWebSocketTask <7AA472C9-8847-4D10-B15D-EA7C970BE6F0>.<1>"), _NSURLErrorFailingURLSessionTaskErrorKey=LocalWebSocketTask <7AA472C9-8847-4D10-B15D-EA7C970BE6F0>.<1>}

As mentioned in the first post, this call succeeds on the simulator but fails on a real device running watchOS 9. This is affecting FirebaseDatabase from functioning properly on watchOS (https://github.com/firebase/firebase-ios-sdk/issues/10195).

I am also experiencing this issues in a brand watch project running on Xcode 14 RC and Watch OS 9. Looking for a fix for this as soon as possible.

One thing that is very interesting though is that a standard HTTP Request using URLSession works fine from the watch. But Websocket gets the above errors.

Same issue here :( All regular URL requests are working fine and the same error as posted by ryanwilson for the sockets. Worked flawlessly on prior watchOS versions and still works on the OS9 simulator

All regular URL requests are working fine and the same error as posted by ryanwilson for the sockets.

This is because watchOS distinguishes between low- and high-level networking, and puts serious restrictions on the former. Matt described this a while back here on DevForums. Lots more folks have bumped into this on watchOS 9 because we fixed a bug where it wasn’t enforcing these rules in all circumstances. Given that, we ‘upgraded’ Matt’s post to a technote, namely TN3135 Low-level networking on watchOS.

Worked flawlessly on prior watchOS versions

If you think it worked “flawlessly” on prior versions of watchOS then you weren’t testing your code adequately )-: Specifically, if you run your code on a watchOS 8 device where the paired iPhone isn’t available, it’ll fail in the same way.

still works on the OS9 simulator

Do not use the simulator as your reference when testing networking code. The simulator uses the macOS networking stack, which means it doesn’t replicate many behaviours you see on iOS, watchOS, and tvOS.

As to how you should proceed here, that depends on your specific circumstances. There are lots of folks on this thread and I think it’d be confusing if I try to respond to all of your issues in a single place. I’d like to reserve this thread for the big picture issue and, if you want specific advice about your situation, start a new thread with your details. Make sure to tag it with the framework you’re using — Foundation, CFNetwork or Network — so that I see your post. Include a link to this thread for context. Oh, and if you want to add a link to your new thread in this context, just drop it in the comments on this post.

Share and Enjoy

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

Just read apple low-level networking on watchOS policy 

https://developer.apple.com/documentation/technotes/tn3135-low-level-networking-on-watchos

And some discussion about restriction for Websocket  using on watchOS 

https://developer.apple.com/forums/thread/679628

So I know the situation is, 

“There’s no supported way to implement WebSocket on watchOS except in the context streaming audio.”

And my App is a voice interaction based app(just like Siri), user can speak to our app to get real-time feedback.

So we need to do ASR(automatic speech recognition) to user speak audio data first. Our current solution is  record user speak audio and transfer audio data to our ASR server by websocket.

(We use Starscream https://github.com/daltoniam/Starscream, to connect websocket)

It works well on IOS and watchOS 8.7, but can not connect to our ASR server after watchOS 9.0.

I think our app is kind of “streaming audio” app,  Just wandering,  is it possible to use websocket in my case, should I try URLSessionWebSocketTask to implement the audio data transfer? Or I can’t implement this feature by websocket at all ?

Thanks.

It works well on iOS and watchOS 8.7

Did it? My experience is that code like this also fails on earlier releases of watchOS if the paired iPhone in unavailable. Did you test that case?

And that suggests a potential ‘fix’, namely, to use Watch Connectivity to pass the request to the paired iPhone and have it do the work on your behalf.

Share and Enjoy

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

I still get ENETDOWN error after I launched CallKit, and sometime it works well. What should I do after func provider(_ provider: CXProvider, didActivate audioSession: AVAudioSession) been called?

Thanks.

I still get ENETDOWN error after I launched CallKit

I recommend that you start a new thread for this issue, tagging it with CallKit so that relevant folks see it.

And, as always, if no one chimes in here, you can open a DTS tech support incident and talk to DTS’s CallKit specialist.

Share and Enjoy

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

WebSocket connection fails in WatchOS9
 
 
Q