Skip to content

Replace NWWebsocket with URLSessionWebSocketTask #442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

theolampert
Copy link

This removes the NWWebsocket client and replaces it with a wrapper around the newer, higher-level URLSessionWebSocketTask. There's still a bit of cleanup to do in this branch, and I'm unsure if it's something you'll want to accept, but I am opening a draft for feedback.

Copy link

stale bot commented Apr 25, 2025

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you'd like this issue to stay open please leave a comment indicating how this issue is affecting you. Thank you.

@stale stale bot added the wontfix label Apr 25, 2025
@theolampert
Copy link
Author

This issue still exists

@stale stale bot removed the wontfix label Apr 26, 2025
@aonemd aonemd requested a review from Copilot June 13, 2025 07:43
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR replaces the legacy NWWebSocket client with a wrapper around URLSessionWebSocketTask. Key changes include updating test cases and mocks from NWWebSocket to WebSocketClient, modifying production code to use URLSessionWebSocketTask-based connection handling, and removing the NWWebSocket dependency from Package.swift.

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
Tests/Unit/Services/WebSocketClientTests.swift Updates tests to work with the new WebSocketClient and URLSessionWebSocketTask API
Tests/Unit/Services/Server/WebSocketServerConnection.swift Minor adjustments in connection handling with NWConnection
Tests/Unit/Services/Server/NWWebSocketServer.swift Replaces NWWebSocket with URLSessionWebSocketTask-based logic; note a potential naming typo
Tests/Unit/Protocols/PusherConnectionDelegateTests.swift Updates disconnect code to use URLSessionWebSocketTask.CloseCode
Tests/Helpers/Mocks.swift Replaces NWWebSocket with WebSocketClient in mocks
Sources/Services/WebSocketClient.swift Implements the new client using URLSessionWebSocketTask and updates related documentation
Sources/Services/PusherConnection.swift Updates socket property type and references to use WebSocketClient
Sources/PusherSwift.swift Updates connection initialization to use the WebSocketClient
Sources/Protocols/WebSocketConnection.swift Defines protocol methods with the new closeCode type
Sources/Extensions/PusherConnection+WebsocketDelegate.swift Changes delegate methods to work with URLSessionWebSocketTask.CloseCode
Package.swift Removes NWWebSocket dependency
Comments suppressed due to low confidence (1)

Sources/Services/WebSocketClient.swift:27

  • The doc comment still refers to NWWebSocket; update it to reflect that this initializer now creates a WebSocketClient instance using URLSessionWebSocketTask.
/// Creates a `NWWebSocket` instance which connects to a socket `url`.

Comment on lines +158 to +161
if Self.receivedPongTimestamps.count == Self.expectedReceivedPongsCount {
Self.pingsWithIntervalExpectation?.fulfill()
}
Self.receivedPongTimestamps.append(Date())
Copy link
Preview

Copilot AI Jun 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider appending the Date timestamp before checking the received count to ensure the condition accurately reflects the number of pong responses received.

Suggested change
if Self.receivedPongTimestamps.count == Self.expectedReceivedPongsCount {
Self.pingsWithIntervalExpectation?.fulfill()
}
Self.receivedPongTimestamps.append(Date())
Self.receivedPongTimestamps.append(Date())
if Self.receivedPongTimestamps.count == Self.expectedReceivedPongsCount {
Self.pingsWithIntervalExpectation?.fulfill()
}

Copilot uses AI. Check for mistakes.

@theolampert theolampert marked this pull request as ready for review June 17, 2025 08:10
@aonemd
Copy link
Member

aonemd commented Jun 17, 2025

Hey @theolampert Thanks for opening the PR 🙌
I would love to test and merge it. And again, I apologize for taking such a long time to pick it up.

I did some research, and found this part in a Pusher blog post where it's mentioned we couldn't use URLSessionWebSocketTask because Pusher protocol defines its own custom codes:

The complication at Pusher is that for Channels, we define several custom closure codes describing reasons why a connection could have been closed by our servers. The single omission from the URLSessionWebSocketTask API is the fact that it does not support custom closure codes.

Although, from my limited understanding, the client code in NWWebSocket, is NOT really sending any custom codes and is using the NWProtocolWebSocket.CloseCode codes which seems it's the same enum as URLSessionWebSocketTask.CloseCode.

  • Is my understanding correct? EDIT: My assumption was wrong. NWWebSocket does indeed support sending custom codes in func disconnect().
  • Have you noticed any issues with reconnection behavior?
  • Are you using any Pusher features that depend on custom close codes?

@theolampert
Copy link
Author

Hey @aonemd I never saw that blog post but that explains better why there was custom handling, we haven't noticed anything strange with reconnection but the reasons to not use URLSessionWebSocketTask make sense. I'll double check what I did here, it was a while go, and see if its indeed a problem. I saw you opened another PR to address the crashes so I'll take a look at that as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants