---
title: "urlSession(_:webSocketTask:didOpenWithProtocol:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/urlsessionwebsocketdelegate/urlsession(_:websockettask:didopenwithprotocol:)"
---

# urlSession(_:webSocketTask:didOpenWithProtocol:)

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

## Declaration

```swift
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

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

## See Also

### Handling WebSocket lifecycle events

- [urlSession(_:webSocketTask:didCloseWith:reason:)](foundation/urlsessionwebsocketdelegate/urlsession(_:websockettask:didclosewith:reason:).md)
