<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.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/receive(completionHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:So25NSURLSessionWebSocketTaskC10FoundationE7receive17completionHandleryys6ResultOyAbCE7MessageOs5Error_pGc_tF"
  },
  "title" : "receive(completionHandler:)"
}
-->

# receive(completionHandler:)

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

```
@preconcurrency func receive(completionHandler: @escaping @Sendable (Result<URLSessionWebSocketTask.Message, any Error>) -> Void)
```

## Parameters

`completionHandler`

A closure that receives two parameters: the WebSocket message, and an [`NSError`](/documentation/Foundation/NSError) that indicates an error encountered while receiving the message. The error is `nil` if no error occurred.

## Discussion

If the task reaches the [`maximumMessageSize`](/documentation/Foundation/URLSessionWebSocketTask/maximumMessageSize) while buffering the frames, this call fails with an error.

---

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)