---
title: "receive(completionHandler:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/urlsessionwebsockettask/receive(completionhandler:)"
---

# receive(completionHandler:)

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

## Declaration

```swift
@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 doc://com.apple.foundation/documentation/Foundation/NSError that indicates an error encountered while receiving the message. The error is nil if no error occurred.

## Discussion

Discussion If the task reaches the maximumMessageSize while buffering the frames, this call fails with an error.

## See Also

### Sending and receiving data

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