Tuesday, June 25, 2024
HomeIOS DevelopmentSwiftNIO tutorial - The echo server

SwiftNIO tutorial – The echo server

Intoducing SwiftNIO

In the event you used a excessive stage internet framework, comparable to Vapor, up to now, you would possibly had some interplay with occasion loops or guarantees. Nicely, these basic constructing blocks are a part of a low stage community framework, referred to as SwiftNIO, which I’ll speak about on this tutorial.

Don’t fret if you have not heard about occasion loops or non-blocking IO simply but, I will attempt to clarify all the pieces on this information, so hopefully you may perceive all the pieces even if you’re a whole newbie to this matter. Let’s begin with some fundamentals about networks and computer systems.

Let’s speak about TCP/IP

It began on January 1st, 1983. The web was born (as some say) and other people began to formally use the web protocol suite (TCP/IP) to speak between gadgets. If you do not know a lot about TCP/IP and you might be curious in regards to the underlying elements, you may learn a number of different articles, however in a nutshell this mannequin permits us to speak with distant computer systems simply. 💬

For example that you’ve got two machines, related by the community. How do they impart with one another? Nicely, identical to while you ship a daily letter, first it’s a must to specify the handle of the recipient. With a purpose to ship a message to a different pc, it’s a must to know its digital handle too. This digital handle is known as IP handle and it appears to be like like this:

So you’ve got bought the handle, however generally this isn’t sufficient, as a result of a constructing can have a number of flats and it’s a must to specify the precise letterbox with the intention to attain the precise particular person. This could occur with computer systems too, the letterbox is known as port quantity and the complete handle of the goal may be created by combining the IP handle and the port quantity (we name this full handle as a community socket handle or just socket, e.g. 💌

After you’ve got specified the precise handle, you may want somebody to truly ship the letter containing your message. The postal supply service can switch your letter, there are two methods to ship it over to the recipient. The primary resolution is to easily ship it with out figuring out a lot in regards to the supply standing, the digital model of this method is known as Person Datagram Protocol (UDP).

The opposite (extra dependable) technique is to get a receipt in regards to the supply, this fashion you may guarantee that the letter truly arrived and the recipient bought it. Though, the postman can open your letter and alter your message, nevertheless it’ll be nonetheless delivered and you will get a notification about this. Once you talk by means of the community, this technique is known as Transmission Management Protocol (TCP).

Okay, that is greater than sufficient community idea, I do know it is a excessive stage abstraction and never fully correct, however hopefully you may get the fundamental concept. Now let’s speak about what occurs contained in the machine and the way we will place an precise digital letterbox in entrance of the imaginary home. 📪

The fundamental constructing blocks of SwiftNIO

What do you do should you anticipate a letter? Aside from the thrill, most individuals always examine their mailboxes to see if it is already there or not. They’re listening for the noises of the postman, identical to pc packages hear on a given port to examine if some knowledge arrived or not. 🤓

What occurs if a letter arrives? Initially it’s a must to go and get it out from the mailbox. With a purpose to get it it’s a must to stroll by means of the hallway or down the steps or you may ask another person to ship the letter for you. Anyway, ought to get the letter by some means first, then based mostly on the envelope you may carry out an motion. If it appears to be like like a spam, you may throw it away, but when it is an vital letter you may almost definitely open it, learn the contents and ship again a solution as quickly as attainable. Let’s follow this analogy, and let me clarify this once more, however this time utilizing SwiftNIO phrases.


A Channel connects the underlying community socket with the applying’s code. The channel’s accountability is to deal with inbound and outbound occasions, taking place by means of the socket (or file descriptor). In different phrases, it is the channel that connects the mailbox with you, you must think about it because the hallway to the mailbox, actually the messages are going journey to you through a channel. 📨


The ChannelPipeline describes a set of actions about deal with the letters. One attainable model is to decide based mostly on the envelope, you may throw it away if it appears to be like like a spam, or open it if it appears to be like like a proper letter, it is also an motion should you reply to the letter. Actions are referred to as as channel handlers in SwiftNIO. In brief: a pipeline is a predefined sequence of handlers.


The ChannelHandler is the motion which you can carry out while you open the letter. The channel handler has an enter and an output kind, which you should use to learn the message utilizing the enter and reply to it utilizing the output. Okay, simply two extra vital phrases, bear with me for a second, I’ll present you some actual examples afterwards. 🐻


The EventLoop works identical to a run loop or a dispatch queue. What does this imply?

The occasion loop is an object that waits for occasions (normally I/O associated occasions, comparable to “knowledge obtained”) to occur after which fires some form of callback once they do.

The trendy CPUs have a restricted variety of cores, apps will almost definitely affiliate one thread (of execution) per core. Switching between thread contexts can be inefficient. What occurs when an occasion has to attend for one thing and a thread turns into accessible for different duties? In SwiftNIO the occasion loop will obtain the incoming message, course of it, and if it has to attend for one thing (like a file or database learn) it’s going to execute another duties within the meantime. When the IO operation finishes it’s going to swap again to the duty and it will name again to your code when it is time. Or one thing like this, however the principle takeaway right here is that your channel handler is at all times going to be related to precisely one occasion loop, this implies actions shall be executed utilizing the identical context.


The EventLoopGroup manages threads and occasion loops. The MultiThreadedEventLoopGroup goes to steadiness out shopper over the accessible threads (occasion loops) this fashion the applying goes to be environment friendly and each thread will deal with nearly the identical quantity of shoppers.

Different elements

There are another SwiftNIO elements, we may speak extra about Futures, Guarantees and the ByteBuffer kind, however I suppose this was greater than sufficient idea for now, so I am not going to dive into these form of objects, however spare them for upcoming articles. 😇

Constructing an echo server utilizing SwiftNIO

You can begin by creating a brand new executable Swift package deal, utilizing the Swift Bundle Supervisor. Subsequent it’s a must to add SwiftNIO as a package deal dependency contained in the Bundle.swift file.

import PackageDescription

let package deal = Bundle(
    title: "echo-server",
    platforms: [
    dependencies: [
            url: "https://github.com/apple/swift-nio",
            from: "2.0.0"
    targets: [
            name: "Server",
            dependencies: [
                    name: "NIO",
                    package: "swift-nio"

The subsequent step is to change the principle mission file, we will simply create the SwiftNIO based mostly TCP server through the use of the ServerBootstrap object. First now we have to instantiate a MultiThreadedEventLoopGroup with a variety of threads, utilizing the CPU cores within the system.

Then we configure the server by including some channel choices. You do not have to know a lot about these simply but, the attention-grabbing half is contained in the childChannelInitializer block. We create the precise channel pipeline there. Our pipeline will encompass two handlers, the primary one is the built-in BackPressureHandler, the second goes to be our customized made EchoHandler object.

In case you are within the accessible ChannelOptions, you may check out the NIO supply code, it additionally incorporates some superb docs about this stuff. The ultimate step is to bind the server bootstrap object to a given host and port, and await incoming connections. 🧐

import NIO

public struct Server {
    public static func fundamental() throws {
        let eventLoopGroup = MultiThreadedEventLoopGroup(
            numberOfThreads: System.coreCount

        defer {
            attempt! eventLoopGroup.syncShutdownGracefully()

        let serverBootstrap = ServerBootstrap(
            group: eventLoopGroup
            worth: 256
            worth: 1
        .childChannelInitializer { channel in
            worth: 1
            worth: 16
            worth: AdaptiveRecvByteBufferAllocator()

        let defaultHost = "" 
        let defaultPort = 8888

        let channel = attempt serverBootstrap.bind(
            host: defaultHost,
            port: defaultPort

        print("Server began and listening on (channel.localAddress!)")
        attempt channel.closeFuture.wait()
        print("Server closed")

As I discussed this, with the intention to deal with an occasion taking place on the channel now we have can create a customized ChannelInboundHandler object. Contained in the channelRead operate it’s attainable to unwrap the inbound knowledge right into a ByteBuffer object and write the enter message onto the output as a wrapped NIOAny object.

Problem: write a server that may print colourful messages. Trace: constructing a textual content modifying server.

import NIO

remaining class EchoHandler: ChannelInboundHandler {

    typealias InboundIn = ByteBuffer
    typealias OutboundOut = ByteBuffer

    func channelRead(
        context: ChannelHandlerContext,
        knowledge: NIOAny
    ) {
        let enter = self.unwrapInboundIn(knowledge)
            let message = enter.getString(at: 0, size: enter.readableBytes)
        else {
        var buff = context.channel.allocator.buffer(capability: message.depend)
        context.write(wrapOutboundOut(buff), promise: nil)

    func channelReadComplete(
        context: ChannelHandlerContext
    ) {

    func errorCaught(
        context: ChannelHandlerContext,
        error: Error
    ) {

        context.shut(promise: nil)

In the event you run the app and hook up with it utilizing the telnet 8888 command you may enter some textual content and the server will echo it again to you. Take into account that this can be a quite simple TCP server, with out HTTP, however it’s attainable to write down express-like HTTP servers, JSON API servers, even a recreation backend and plenty of different cool and loopy performant stuff utilizing SwiftNIO. I hope this tutorial will assist you to get began with SwiftNIO, I am additionally studying lots in regards to the framework currently, so please forgive me (and even right me) if I missed / tousled one thing. 😅

So once more: SwiftNIO a (low-level) non-blocking event-driven community utility framework for prime efficiency protocol servers & shoppers. It is like Netty, however written for Swift.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments