NIOAsyncChannel

Wraps a NIO Channel object into a form suitable for use in Swift Concurrency.

AsyncChannel.swift:36
iOS
13+
macOS
10.15+
tvOS
13+
watchOS
6+
struct NIOAsyncChannel<Inbound, Outbound> where Inbound : Sendable, Outbound : Sendable

NIOAsyncChannel abstracts the notion of a NIO Channel into something that can safely be used in a structured concurrency context. In particular, this exposes the following functionality:

  • reads are presented as an AsyncSequence

  • writes can be written to with async functions on a writer, providing back pressure

  • channels can be closed seamlessly

This type does not replace the full complexity of NIO’s Channel. In particular, it does not expose the following functionality:

  • user events

  • traditional NIO back pressure such as writability signals and the channel’s read call

Users are encouraged to separate their ChannelHandlers into those that implement protocol-specific logic (such as parsers and encoders) and those that implement business logic. Protocol-specific logic should be implemented as a ChannelHandler, while business logic should use NIOAsyncChannel to consume and produce data to the network.