<!--
{
  "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/URLSession/webSocketTask(with:protocols:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSURLSession(im)webSocketTaskWithURL:protocols:"
  },
  "title" : "webSocketTask(with:protocols:)"
}
-->

# webSocketTask(with:protocols:)

Creates a WebSocket task given a URL and an array of protocols.

```
func webSocketTask(with url: URL, protocols: [String]) -> URLSessionWebSocketTask
```

## Parameters

`url`

The WebSocket URL with which to connect.

`protocols`

An array of protocols to negotiate with the server.

## Discussion

During the WebSocket handshake, the task uses the provided protocols to negotiate a preferred protocol with the server.

> Note:
> The protocol doesn’t affect the WebSocket framing. More details on the protocol are available in [RFC 6455, The WebSocket Protocol](https://tools.ietf.org/html/rfc6455).

---

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)