Sunday, March 26, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home Swift

SwiftNIO tutorial – The echo server

learningcode_x1mckf by learningcode_x1mckf
January 27, 2023
in Swift
0
SwiftNIO tutorial – The echo server
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


You might also like

Introducing – AI Utils for macOS

Encoding and decoding data using the Hummingbird framework

Hummingbird routing and requests – The.Swift.Dev.

Intoducing SwiftNIO


In the event you used a excessive degree net framework, corresponding to Vapor, up to now, you would possibly had some interplay with occasion loops or guarantees. Effectively, these elementary constructing blocks are a part of a low degree community framework, known as SwiftNIO, which I will speak about on this tutorial.


Don’t be concerned if you have not heard about occasion loops or non-blocking IO simply but, I will attempt to clarify the whole lot on this information, so hopefully you may perceive the whole lot even in case you are an entire 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 internet was born (as some say) and other people began to formally use the internet 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 components, you’ll be able to learn a couple of different articles, however in a nutshell this mannequin permits us to speak with distant computer systems simply. 💬


As an instance that you’ve got two machines, linked by the community. How do they convey with one another? Effectively, similar to if you ship a daily letter, first you must specify the handle of the recipient. With a purpose to ship a message to a different laptop, you must know its digital handle too. This digital handle known as IP address and it seems like this: 127.0.0.1.


So you have bought the handle, however typically this isn’t sufficient, as a result of a constructing can have a number of flats and you must specify the precise letterbox so as to attain the precise individual. This may occur with computer systems too, the letterbox known as port quantity and the total handle of the goal may be created by combining the IP handle and the port quantity (we name this full handle as a network socket handle or just socket, e.g. 127.0.0.1:80). 💌


After you have 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 strategy known as User Datagram Protocol (UDP).


The opposite (extra dependable) technique is to get a receipt in regards to the supply, this fashion you’ll be able to make it possible for the letter really arrived and the recipient bought it. Though, the postman can open your letter and alter your message, however it’ll be nonetheless delivered and you will get a notification about this. While you talk by the community, this technique known as Transmission Control Protocol (TCP).


Okay, that is greater than sufficient community concept, I do know it is a excessive degree abstraction and never fully correct, however hopefully you may get the essential 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 when you count on a letter? Aside from the joy, most individuals consistently test their mailboxes to see if it is already there or not. They’re listening for the noises of the postman, similar to laptop applications hear on a given port to test if some information arrived or not. 🤓


What occurs if a letter arrives? To start with you must go and get it out from the mailbox. With a purpose to get it you must stroll by the hallway or down the steps or you’ll be able to ask another person to ship the letter for you. Anyway, ought to get the letter one way or the other first, then primarily based on the envelope you’ll be able to carry out an motion. If it seems like a spam, you may throw it away, but when it is an vital letter you may more than likely open it, learn the contents and ship again a solution as quickly as attainable. Let’s keep on with this analogy, and let me clarify this once more, however this time utilizing SwiftNIO phrases.


Channel


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


ChannelPipeline

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


ChannelHandler

The ChannelHandler is the motion which you could carry out if 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 will present you some actual examples afterwards. 🐻


EventLoop

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


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


The trendy CPUs have a restricted variety of cores, apps will more than likely affiliate one thread (of execution) per core. Switching between thread contexts can also be inefficient. What occurs when an occasion has to attend for one thing and a thread turns into obtainable 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 will execute another duties within the meantime. When the IO operation finishes it will 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 all the time going to be related to precisely one occasion loop, this implies actions shall be executed utilizing the identical context.


EventLoopGroup

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


Different parts

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


Constructing an echo server utilizing SwiftNIO


You can begin by creating a brand new executable Swift bundle, utilizing the Swift Package Manager. Subsequent you must add SwiftNIO as a bundle dependency contained in the Bundle.swift file.



import PackageDescription

let bundle = Bundle(
    title: "echo-server",
    platforms: [
       .macOS(.v10_15),
    ],
    dependencies: [
        .package(
            url: "https://github.com/apple/swift-nio",
            from: "2.0.0"
        ),
    ],
    targets: [
        .executableTarget(
            name: "Server",
            dependencies: [
                .product(
                    name: "NIO",
                    package: "swift-nio"
                )
            ]
        ),
    ]
)


The subsequent step is to change the principle undertaking file, we will simply create the SwiftNIO primarily based TCP server through the use of the ServerBootstrap object. First we’ve to instantiate a MultiThreadedEventLoopGroup with various 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 obtainable ChannelOptions, you’ll be able to check out the NIO supply code, it additionally accommodates some superb docs about this stuff. The ultimate step is to bind the server bootstrap object to a given host and port, and look forward to incoming connections. 🧐


import NIO

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

        defer 
            attempt! eventLoopGroup.syncShutdownGracefully()
        

        let serverBootstrap = ServerBootstrap(
            group: eventLoopGroup
        )
        .serverChannelOption(
            ChannelOptions.backlog,
            worth: 256
        )
        .serverChannelOption(
            ChannelOptions.socketOption(.so_reuseaddr),
            worth: 1
        )
        .childChannelInitializer  channel in
            channel.pipeline.addHandlers([
                BackPressureHandler(),
                EchoHandler(),
            ])
        
        .childChannelOption(
            ChannelOptions.socketOption(.so_reuseaddr),
            worth: 1
        )
        .childChannelOption(
            ChannelOptions.maxMessagesPerRead,
            worth: 16
        )
        .childChannelOption(
            ChannelOptions.recvAllocator,
            worth: AdaptiveRecvByteBufferAllocator()
        )

        let defaultHost = "127.0.0.1" 
        let defaultPort = 8888

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

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


As I discussed this, so as to deal with an occasion taking place on the channel we’ve can create a customized ChannelInboundHandler object. Contained in the channelRead operate it’s attainable to unwrap the inbound information 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: building a text modifying server.


import NIO

ultimate class EchoHandler: ChannelInboundHandler 

    typealias InboundIn = ByteBuffer
    typealias OutboundOut = ByteBuffer

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


    func channelReadComplete(
        context: ChannelHandlerContext
    ) 
        context.flush()
    

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

        context.shut(promise: nil)
    


In the event you run the app and hook up with it utilizing the telnet 127.0.0.1 8888 command you’ll be able to enter some textual content and the server will echo it again to you. Understand that it is a quite simple TCP server, with out HTTP, however it’s attainable to jot down express-like HTTP servers, JSON API servers, even a game backend and plenty of different cool and loopy performant stuff utilizing SwiftNIO. I hope this tutorial will assist you to to get began with SwiftNIO, I am additionally studying lots in regards to the framework currently, so please forgive me (and even appropriate 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 & purchasers. It is like Netty, however written for Swift.




Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

Introducing – AI Utils for macOS

by learningcode_x1mckf
March 24, 2023
0
Introducing – AI Utils for macOS

⚠️ REQUIRES a FREE OpenAI API key. You will get one utilizing this hyperlink. ⚠️ Improve your productiveness with our AI powered utility application. - accessible...

Read more

Encoding and decoding data using the Hummingbird framework

by learningcode_x1mckf
March 22, 2023
0
Encoding and decoding data using the Hummingbird framework

HTTP is all about sending and receiving information over the community. Initially it was solely utilized to switch HTML paperwork, however these days we use HTTP to switch...

Read more

Hummingbird routing and requests – The.Swift.Dev.

by learningcode_x1mckf
March 17, 2023
0
Hummingbird routing and requests – The.Swift.Dev.

Routing on the server facet means the server goes to ship a response primarily based on the URL path that the consumer referred to as when firing up...

Read more

Building a Scrollable Custom Tab Bar in SwiftUI

by learningcode_x1mckf
March 10, 2023
0
Building a Scrollable Custom Tab Bar in SwiftUI

Whether or not you’re making a social media app or a productiveness device, the tab bar interface can improve the consumer expertise by making it extra intuitive and...

Read more

Beginner’s guide to server-side Swift using the Hummingbird framework

by learningcode_x1mckf
March 8, 2023
0
Beginner’s guide to server-side Swift using the Hummingbird framework

Swift on the Server in 2023 Three years in the past I began to focus on Vapor, the preferred web-framework written in Swift, which served me very...

Read more
Next Post
Solutions Architect Java JavaScript Cloud – Semi Remote – R850 per hour at e-Merge IT Recruitment

NEW WORK: Senior C# JavaScript Developer @ Remote dev shop - R850k at e-Merge IT Recruitment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Learn C++ With Udacity – 70% Off – iProgrammer

February 13, 2023
Get this 8-course 2023 Premium C Programming Developer Bundle for just $40

Get this 8-course 2023 Premium C Programming Developer Bundle for just $40

January 15, 2023
5 fun ways to brush up on your JavaScript skills

5 fun ways to brush up on your JavaScript skills

November 4, 2022

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • 2023 Java roadmap for developers – TheServerSide.com
  • YS Jagan launches Ragi Java in Jagananna Gorumudda, says focused on intellectual development of students – The Hans India
  • Disadvantages of Java – TheServerSide.com

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?