---
title: "webSocketTask(with:protocols:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/urlsession/websockettask(with:protocols:)"
---

# webSocketTask(with:protocols:)

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

## Declaration

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

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.

## See Also

### Adding WebSocket tasks to a session

- [webSocketTask(with:)](foundation/urlsession/websockettask(with:)-87ipz.md)
- [webSocketTask(with:)](foundation/urlsession/websockettask(with:)-mtks.md)
- [URLSessionWebSocketTask](foundation/urlsessionwebsockettask.md)
- [URLSessionWebSocketDelegate](foundation/urlsessionwebsocketdelegate.md)
