<!--
{
  "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",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(pl)NSURLSessionWebSocketDelegate"
  },
  "title" : "URLSessionWebSocketDelegate"
}
-->

# URLSessionWebSocketDelegate

A protocol that defines methods that URL session instances call on their delegates to handle task-level events specific to WebSocket tasks.

```
protocol URLSessionWebSocketDelegate : URLSessionTaskDelegate
```

## Topics

### Handling WebSocket lifecycle events

[`urlSession(_:webSocketTask:didOpenWithProtocol:)`](/documentation/Foundation/URLSessionWebSocketDelegate/urlSession(_:webSocketTask:didOpenWithProtocol:))

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

[`urlSession(_:webSocketTask:didCloseWith:reason:)`](/documentation/Foundation/URLSessionWebSocketDelegate/urlSession(_:webSocketTask:didCloseWith:reason:))

Tells the delegate that the WebSocket task received a close frame from the server endpoint, optionally including a close code and reason from the server.



---

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)