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

# webSocketTask(with:)

Creates a WebSocket task for the provided URL request.

## Declaration

```swift
func webSocketTask(with request: URLRequest) -> URLSessionWebSocketTask
```

## Parameters

- `request`: A URL request that indicates a WebSockets endpoint with which to connect.

## Discussion

Discussion You can modify the request’s properties prior to calling resume() on the task. The task uses these properties during the HTTP handshake phase. To add custom protocols, add a header with the key Sec-WebSocket-Protocol, and a comma-separated list of protocols you want to negotiate with the server. The custom HTTP headers provided by the client remain unchanged for the handshake with the server.

## See Also

### Adding WebSocket tasks to a session

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