---
title: URLSessionWebSocketTask
framework: foundation
role: symbol
role_heading: Class
path: foundation/urlsessionwebsockettask
---

# URLSessionWebSocketTask

A URL session task that communicates over the WebSockets protocol standard.

## Declaration

```swift
class URLSessionWebSocketTask
```

## Overview

Overview URLSessionWebSocketTask is a concrete subclass of URLSessionTask that provides a message-oriented transport protocol over TCP and TLS in the form of WebSocket framing. It follows the WebSocket Protocol defined in RFC 6455. You create a URLSessionWebSocketTask with either a ws: or wss: URL. When creating the task, you can also provide a list of protocols to advertise during the handshake phase. Once the handshake completes, your app receives notifications through the session’s delegate. You send data with send(_:completionHandler:) and receive data with receive(completionHandler:). The task performs reads and writes asynchronously, and allows you to send and receive messages that contain both binary frames and UTF-8 encoded text frames. The task enqueues any reads or writes you perform prior to the handshake’s completion, and executes them after the handshake completes. URLSessionWebSocketTask supports redirection and authentication like other types of tasks do, using the methods in URLSessionTaskDelegate. The WebSocket task calls the redirection and authentication delegate methods prior to completing the handshake. The WebSocket task also supports cookies, by storing cookies to the session configuration’s httpCookieStorage, and attaches cookies to outgoing HTTP handshake requests. note: watchOS supports URLSessionWebSocketTask for specific use cases. For more details, see TN3135: Low-level networking on watchOS.

## Topics

### Sending and receiving data

- [send(_:completionHandler:)](foundation/urlsessionwebsockettask/send(_:completionhandler:).md)
- [URLSessionWebSocketTask.Message](foundation/urlsessionwebsockettask/message.md)
- [receive(completionHandler:)](foundation/urlsessionwebsockettask/receive(completionhandler:).md)
- [maximumMessageSize](foundation/urlsessionwebsockettask/maximummessagesize.md)

### Sending ping frames

- [sendPing(pongReceiveHandler:)](foundation/urlsessionwebsockettask/sendping(pongreceivehandler:).md)

### Closing the connection

- [cancel(with:reason:)](foundation/urlsessionwebsockettask/cancel(with:reason:).md)
- [closeCode](foundation/urlsessionwebsockettask/closecode-swift.property.md)
- [URLSessionWebSocketTask.CloseCode](foundation/urlsessionwebsockettask/closecode-swift.enum.md)
- [closeReason](foundation/urlsessionwebsockettask/closereason.md)

### Instance Methods

- [receive()](foundation/urlsessionwebsockettask/receive().md)
- [send(_:)](foundation/urlsessionwebsockettask/send(_:).md)

## Relationships

### Inherits From

- [URLSessionTask](foundation/urlsessiontask.md)

### Conforms To

- [CVarArg](swift/cvararg.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Hashable](swift/hashable.md)
- [NSCopying](foundation/nscopying.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)
- [ProgressReporting](foundation/progressreporting.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)

## See Also

### Adding WebSocket tasks to a session

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