<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/URLSessionWebSocketDelegate/urlSession(_:webSocketTask:didOpenWithProtocol:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(pl)NSURLSessionWebSocketDelegate(im)URLSession:webSocketTask:didOpenWithProtocol:"
  },
  "title" : "urlSession(_:webSocketTask:didOpenWithProtocol:)"
}
-->

# urlSession(_:webSocketTask:didOpenWithProtocol:)

Tells the delegate that the WebSocket task successfully negotiated the handshake with the endpoint, indicating the negotiated protocol.

```
optional func urlSession(_ session: URLSession, webSocketTask: URLSessionWebSocketTask, didOpenWithProtocol protocol: String?)
```

## Parameters

`session`

The session of the WebSocket task that opened.

`webSocketTask`

The WebSocket task that opened.

`protocol`

The protocol picked during the handshake phase. This parameter is `nil` if the server did not pick a protocol, or if the client did not advertise protocols when creating the task.

## Discussion

If the handshake fails, the task doesn’t call this delegate method.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)