Code Monkey home page Code Monkey logo

swiftnio-asyncawait-example's Introduction

SwiftNIO-AsyncAwait-Example

Demonstrates consuming the SwiftNIO library through simple async/await methods

This code example is a simple TCP client built on SwiftNIO. SwiftNIO predates the concurrency features built into the Swift language, such as the async and await keywords. Although SwiftNIO is being retrofitted to support these language features, this work is not yet complete.1

This example shows an interim approach. Specifically, it adds two async methods to Channel:

func asyncRead() async throws -> ByteBuffer?

func asyncWrite(_ buffer: ByteBuffer) async throws

asyncRead() is implemented by an AsyncThrowingStream. Backpressure is applied to throttle incoming data to keep the number of bytes buffered by the AsyncThrowingStream within a target range, specified by unconsumedBytesLowWatermark and unconsumedBytesHighWatermark.

The implementation of asyncWrite(buffer:) is trivial, since SwiftNIO already supports an async write API.

Usage

This example is a simple client that opens a TCP socket to a server, then reads lines from stdin (the keyboard), writes those lines to the socket, reads data from that socket, and prints that data to stdout. It can be run against the NIOEchoServer or NIOChatServer examples included in SwiftNIO (https://github.com/apple/swift-nio) or the NIOTLSServer example included in https://github.com/apple/swift-nio-ssl.

To run:

  • Start the server of your choice
  • Set the values of host, port, and useSSL
  • From the terminal, swift run to start the client
  • Ctrl+D to exit

Footnotes

  1. If I'm missing something here, please let me know! โ†ฉ

swiftnio-asyncawait-example's People

Contributors

pitfield avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.