SwiftNIO Tutorial: Practical Guide for Asynchronous Problems | raywenderlich.com

In this tutorial, you’ll solve common asynchronous problems about promises and futures in SwiftNIO by building a quotations app.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/8016626-swiftnio-tutorial-practical-guide-for-asynchronous-problems

Build with an error, could you please provide a help.

/code/Sources/PromisesFutures/QuoteResponder.swift:96:21: error: unable to infer complex closure return type; add explicit type to disambiguate
http_1               |     flatMapThrowing { optional in
http_1               |                     ^
http_1               | 
docker_http_1 exited with code 1

@joannis Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hello,

This issue is likely to arise when the Swift toolchain is 5.0 or lower. The implicit return statement that is used was introduced in Swift 5.1. You can fix this on lower versions of Swift by adding the return keyword yourself, or by upgrading the Swift version.

Hello and thank you very much for the great tutorial!
I have two questions on SwiftNIO’s (Vapor’s, respectively) capabilities, maybe you can help:

Am using the Network framework on my iOS App client for creating an NWConnection to my server with DTLS over UDP (I use init(dtls:udp:) for the NWParameters). I am hoping that I can write my server in Swift and I am really happy that you have all these awesome resources on the topic on server-side swift and Vapor. Thanks a lot for these!!!

So I need to be able to accept a DTLS over UDP connection on my server.
My understanding is that SwiftNIO provides a connection for UDP via DataGramBoostrap for a server.

My questions are the following:

  1. How can I set the “dtls option” in SwiftNIO for my UDP server (when I use DataGramBootstrap)?
  2. I understand that Vapor 4 does not yet provide UDP support, is that correct? So in any case I would have to work with the lower level SwiftNIO, is that correct? Is there a plan to include UDP support in Vapor 5?

Thank you very much for your insights!

@joannis Do you have any feedback about this? Thank you - much appreciated! :]

Hello Celine,

Vapor 4 doesn’t provide UDP support, but SwiftNIO does. As SwiftNIO forms much of the foundation for Vapor, I don’t see any issues with using SwiftNIO based UDP servers. However, if you want to run this service on an iOS device, you’ll quickly run into a lack of support inside with SwiftNIO.

SwiftNIO has a fundamental concept of a ‘channel’, which can be used as a transport for anything, usually ByteBuffer when it comes to networking. (Note the usually, since SwiftNIO’s UDP support doesn’t use raw a ByteBuffer). These channels can by implemented by anyone, for many reasons. You could have a VPN implementation, and expose a VPN tunnel as a channel.

To expand upon the POSIX sockets for iOS, the NIO team created SwiftNIOTransportServices, which implements Network.Framework support for SwiftNIO. This build does not yet support UDP, but I’ve made a PR to address that. My PR is functional, but incomplete.

Alternatively, you could copy my UDP support from the PR and use that, but the PR you see unfortunately doesn’t yet support UDP servers.

If you want to use the regular POSIX UDP sockets, you can use that. But I’m not aware of the details of DTLS Support in SwiftNIO’s POSIX UDP services.

Thank you very much Joannis! I will have a look at your PR and go from there.