<!--
{
  "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/URLSessionWebSocketTask",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSURLSessionWebSocketTask"
  },
  "title" : "URLSessionWebSocketTask"
}
-->

# URLSessionWebSocketTask

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

```
class URLSessionWebSocketTask
```

## Overview

[`URLSessionWebSocketTask`](/documentation/Foundation/URLSessionWebSocketTask) is a concrete subclass of [`URLSessionTask`](/documentation/Foundation/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](https://tools.ietf.org/html/rfc6455).

You create a [`URLSessionWebSocketTask`](/documentation/Foundation/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`](/documentation/Foundation/URLSession/delegate).

You send data with [`send(_:completionHandler:)`](/documentation/Foundation/URLSessionWebSocketTask/send(_:completionHandler:)) and receive data with [`receive(completionHandler:)`](/documentation/Foundation/URLSessionWebSocketTask/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`](/documentation/Foundation/URLSessionWebSocketTask) supports redirection and authentication like other types of tasks do, using the methods in [`URLSessionTaskDelegate`](/documentation/Foundation/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`](/documentation/Foundation/URLSessionConfiguration/httpCookieStorage), and attaches cookies to outgoing HTTP handshake requests.

> Note:
> watchOS supports ``doc://com.apple.foundation/documentation/Foundation/URLSessionWebSocketTask`` for specific use cases. For more details, see <doc://com.apple.documentation/documentation/Technotes/tn3135-low-level-networking-on-watchOS>.

## Topics

### Sending and receiving data

[`send(_:completionHandler:)`](/documentation/Foundation/URLSessionWebSocketTask/send(_:completionHandler:))

Sends a WebSocket message, receiving the result in a completion handler.

[`URLSessionWebSocketTask.Message`](/documentation/Foundation/URLSessionWebSocketTask/Message)

An enumeration of the types of messages sent and received.

[`receive(completionHandler:)`](/documentation/Foundation/URLSessionWebSocketTask/receive(completionHandler:))

Reads a WebSocket message once all the frames of the message are available.

[`sendMessage:completionHandler:`](/documentation/Foundation/NSURLSessionWebSocketTask/sendMessage:completionHandler:)

Sends a WebSocket message, receiving the result in a completion handler.

[`receiveMessageWithCompletionHandler:`](/documentation/Foundation/NSURLSessionWebSocketTask/receiveMessageWithCompletionHandler:)

Reads a WebSocket message once all the frames of the message are available.

[`maximumMessageSize`](/documentation/Foundation/URLSessionWebSocketTask/maximumMessageSize)

The maximum number of bytes to buffer before the receive call fails with an error.

### Sending ping frames

[`sendPing(pongReceiveHandler:)`](/documentation/Foundation/URLSessionWebSocketTask/sendPing(pongReceiveHandler:))

Sends a ping frame from the client side, with a closure to receive the pong from the server endpoint.

### Closing the connection

[`cancel(with:reason:)`](/documentation/Foundation/URLSessionWebSocketTask/cancel(with:reason:))

Sends a close frame with the given close code and optional close reason.

[`closeCode`](/documentation/Foundation/URLSessionWebSocketTask/closeCode-swift.property)

A code that indicates the reason a connection closed.

[`URLSessionWebSocketTask.CloseCode`](/documentation/Foundation/URLSessionWebSocketTask/CloseCode-swift.enum)

A code that indicates why a WebSocket connection closed.

[`closeReason`](/documentation/Foundation/URLSessionWebSocketTask/closeReason)

A block of data that provides further information about why a connection closed.



---

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)