London Escorts sunderland escorts 1v1.lol unblocked yohoho 76 https://www.symbaloo.com/mix/yohoho?lang=EN yohoho https://www.symbaloo.com/mix/agariounblockedpvp https://yohoho-io.app/ https://www.symbaloo.com/mix/agariounblockedschool1?lang=EN
3.3 C
New York
Friday, January 31, 2025

Iterating over internet socket messages with async / await in Swift – Donny Wals


Printed on: January 24, 2023

In iOS 13, we gained the power to simply ship and obtain information utilizing internet sockets by means of URLSession. With async/await, we gained the power to fetch information from servers utilizing the await key phrase and we will iterate over asynchronous sequences utilizing async for loops.

We will even learn information from a URL one line at a time by calling the strains property on URL:

let url = URL(string: "https://donnywals.com")!

for strive await line in url.strains {
    // use line
}

Whereas that is actually cool and permits us to construct apps that ingest information in actual time if the server helps streaming our bodies, we can’t use the strains property to arrange an internet socket connection and pay attention for incoming messages and probably ship messages over the identical connection too.

On this put up, you’ll study all the things you should find out about constructing your individual mechanism to conveniently iterate over messages from an internet socket asynchronously. We’ll leverage some current performance from URLSessionWebSocketTask and AsyncThrowingStream to construct our personal AsyncSequence that conveniently wraps our URLSessionWebSocketTask.

Be aware that the ensuing code has solely had comparatively restricted testing finished so I can’t assure that the offered resolution can be 100% right for all the things you throw at it. When you discover any points with the ultimate code, be happy to contact me. Bonus factors should you’re in a position to present some concepts for a possible repair.

Utilizing an internet socket with out async / await

Earlier than we get began, let’s shortly evaluation use an internet socket with out async/await. The code particulars are outlined in this put up. You should definitely learn it if you wish to study extra about utilizing internet sockets in your apps.


let url = URL(string: "ws://127.0.0.1:8080")!
let socketConnection = URLSession.shared.webSocketTask(with: url)
socketConnection.resume()

func setReceiveHandler() {
    socketConnection.obtain { lead to
        defer { self.setReceiveHandler() }

        do {
            let message = strive end result.get()
            change message {
            case let .string(string):
                print(string)
            case let .information(information):
                print(information)
            @unknown default:
                print("unkown message obtained")
            }
        } catch {
            // deal with the error
            print(error)
        }
    }
}

setReceiveHandler()

Discover how, to obtain messages from the socket, I need to name obtain with a completion handler. This methodology solely permits me to obtain a single incoming message, so I need to re-set my handler after receiving a message to mechanically start listening for the subsequent message.

This can be a nice instance of a state of affairs the place an async for loop akin to for strive await message in socketConnection would make lots of sense. Sadly, this isn’t potential out of the field. Nonetheless, URLSessionWebSocketTask supplies some type of help for async / await so we’re not fully out of luck.

A fundamental implementation of internet sockets with async / await

Whereas URLSessionWebSocketTask doesn’t expose an AsyncSequence that emits incoming messages out of the field, it does include an async model of the obtain methodology you noticed earlier.

This permits us to rewrite the instance above as an async methodology as follows:

func setReceiveHandler() async {
    do {
        let message = strive await socketConnection.obtain()

        change message {
        case let .string(string):
          print(string)
        case let .information(information):
          print(information)
        @unknown default:
          print("unkown message obtained")
        }
    } catch {
        print(error)
    }

    await setReceiveHandler()
}

This code works simply fantastic, besides we don’t actually have a method to cease the recursion right here. The code you noticed earlier really has the very same situation; there’s no situation to cease listening for internet socket messages even when the net socket connection has already been closed.

We might enhance our code by solely recursing if:

  1. We didn’t encounter any errors
  2. The socket connection continues to be energetic

This is able to look a bit as follows:

func setReceiveHandler() async {
    guard socketConnection.closeCode == .invalid else {
        return
    }

    do {
        let message = strive await socketConnection.obtain()

        change message {
        case let .string(string):
          print(string)
        case let .information(information):
          print(information)
        @unknown default:
          print("unkown message obtained")
        }

        await setReceiveHandler()
    } catch {
        print(error)
    }
}

An open internet socket’s closed code is at all times mentioned to invalid to sign that the connection has not (but) been closed. We will leverage this to test that our connection continues to be energetic earlier than ready for the subsequent message to be obtained.

That is a lot better already as a result of we respect closed sockets and failures a lot nicer now, however we might enhance the readability of this code a tiny bit by leveraging a whereas loop as an alternative of recursively calling the setReceiveHandler perform:

func setReceiveHandler() async {
    var isActive = true

    whereas isActive && socketConnection.closeCode == .invalid {
        do {
            let message = strive await socketConnection.obtain()

            change message {
            case let .string(string):
              print(string)
            case let .information(information):
              print(information)
            @unknown default:
              print("unkown message obtained")
            }
        } catch {
            print(error)
            isActive = false
        }
    }
}

To me, this model of the code is barely simpler to learn however that may not be the case for you. It’s functionally equal so you possibly can select to make use of whichever choice fits you greatest.

Whereas this code works, I’m not fairly proud of the place we’ve landed proper now. There’s lots of logic on this perform and I would like to separate dealing with the incoming values from the calls to socketConnection.obtain() by some means. Ideally, I ought to have the ability to write the next:

do {
    for strive await message in socketConnection {
        change message {
        case let .string(string):
            print(string)
        case let .information(information):
            print(information)
        @unknown default:
            print("unkown message obtained")
      }
} catch {
    // deal with error
}

That is a lot, a lot nicer from a call-site perspective and it will enable us to place the ugly bits elsewhere.

To do that, we will leverage the facility of AsyncStream which permits us to construct a customized async sequence of values.

Utilizing AsyncStream to emit internet socket messages

Given our finish aim, there are just a few methods for us to get the place we wish to be. The simplest manner can be to write down a perform in an extension on URLSessionWebSocketTask that will encapsulate the whereas loop you noticed earlier. This implementation would look as follows:

typealias WebSocketStream = AsyncThrowingStream<URLSessionWebSocketTask.Message, Error>

public extension URLSessionWebSocketTask {    
    var stream: WebSocketStream {
        return WebSocketStream { continuation in
            Activity {
                var isAlive = true

                whereas isAlive && closeCode == .invalid {
                    do {
                        let worth = strive await obtain()
                        continuation.yield(worth)
                    } catch {
                        continuation.end(throwing: error)
                        isAlive = false
                    }
                }
            }
        }
    }
}

To make the code just a little bit simpler to learn, I’ve outlined a typealias for my AsyncThrowingStream so we don’t have to have a look at the identical lengthy sort signature in all places.

The code above creates an occasion of AsyncThrowingStream that asynchronously awaits new values from the net socket so long as the net socket is taken into account energetic and hasn’t been closed. To emit incoming messages and potential errors, the continuation’s yield and end strategies are used. These strategies will both emit a brand new worth (yield) or finish the stream of values with an error (end).

This code works nice in lots of conditions, however there may be one situation. If we determine to shut the net socket connection from the app’s facet by calling cancel(with:purpose:) on our socketConnection, our WebSocketStream doesn’t finish. As an alternative, it will likely be caught ready for messages, and the decision web site can be caught too.

Activity {
    strive await Activity.sleep(for: .seconds(5))
    strive await socketConnection.cancel(with: .goingAway, purpose: nil)
}

Activity {    
    do {
        for strive await message in socketConnection.stream {
            // deal with incoming messages
        }
    } catch {
        // deal with error
    }

    print("this may by no means be printed")
}

If all the things works as anticipated, our internet socket connection will shut after 5 seconds. At that time, our for loop ought to finish and our print assertion ought to execute, because the asynchronous stream is not energetic. Sadly, this isn’t the case, so we have to discover a higher solution to mannequin our stream.

URLSessionWebSocketTask doesn’t present a manner for us to detect cancellation. So, I’ve discovered that it’s best to make use of an object that wraps the URLSessionWebSocketTask, and to cancel the duty by means of that object. This permits us to each finish the async stream we’re offering to callers and shut the net socket reference to one methodology name.

Right here’s what that object seems like:

class SocketStream: AsyncSequence {
    typealias AsyncIterator = WebSocketStream.Iterator
    typealias Ingredient = URLSessionWebSocketTask.Message

    non-public var continuation: WebSocketStream.Continuation?
    non-public let activity: URLSessionWebSocketTask

    non-public lazy var stream: WebSocketStream = {
        return WebSocketStream { continuation in
            self.continuation = continuation

            Activity {
                var isAlive = true

                whereas isAlive && activity.closeCode == .invalid {
                    do {
                        let worth = strive await activity.obtain()
                        continuation.yield(worth)
                    } catch {
                        continuation.end(throwing: error)
                        isAlive = false
                    }
                }
            }
        }
    }()

    init(activity: URLSessionWebSocketTask) {
        self.activity = activity
        activity.resume()
    }

    deinit {
        continuation?.end()
    }

    func makeAsyncIterator() -> AsyncIterator {
        return stream.makeAsyncIterator()
    }

    func cancel() async throws {
        activity.cancel(with: .goingAway, purpose: nil)
        continuation?.end()
    }
}

There’s a bunch of code right here, nevertheless it’s not too dangerous. The primary few strains are all about establishing some sort aliases and properties for comfort. The lazy var stream is basically the very same code that you just’ve already within the URLSessionWebSocketTask extension from earlier than.

When our SocketStream‘s deinit is named we guarantee that we finish our stream. There’s additionally a cancel methodology that closes the socket connection in addition to the stream. As a result of SocketStream conforms to AsyncSequence we should present an Iterator object that’s used after we attempt to iterate over our SocketStreams. We merely ask our inner stream object to make an iterator and use that as our return worth.

Utilizing the code above seems as follows:

let url = URL(string: "ws://127.0.0.1:8080")!
let socketConnection = URLSession.shared.webSocketTask(with: url)
let stream = SocketStream(activity: socketConnection)

Activity {  
    do {
        for strive await message in stream {
            // deal with incoming messages
        }
    } catch {
        // deal with error
    }

    print("this can be printed as soon as the stream ends")
}

To cancel our stream after 5 seconds similar to earlier than, you possibly can run the next activity in parallel with our iterating activity:

Activity {
    strive await Activity.sleep(for: .seconds(5))
    strive await stream.cancel()
}

Activity {
    // iterate...
}

Whereas that is fairly cool, we do have a little bit of a difficulty right here due to the next little bit of code:

non-public lazy var stream: WebSocketStream = {
    return WebSocketStream { continuation in
        self.continuation = continuation

        Activity {
            var isAlive = true

            whereas isAlive && activity.closeCode == .invalid {
                do {
                    let worth = strive await activity.obtain()
                    continuation.yield(worth)
                } catch {
                    continuation.end(throwing: error)
                    isAlive = false
                }
            }
        }
    }
}()

The duty that we run our whereas loop in received’t finish until we finish our stream from inside our catch block. If we manually shut the net socket connection utilizing the cancel methodology we write earlier, the decision to obtain() won’t ever obtain an error nor a price which implies that it will likely be caught ceaselessly.

Probably the most dependable solution to repair that is to return to the callback based mostly model of obtain to drive your async stream:

non-public lazy var stream: WebSocketStream = {
    return WebSocketStream { continuation in
        self.continuation = continuation
        waitForNextValue()
    }
}()

non-public func waitForNextValue() {
    guard activity.closeCode == .invalid else {
        continuation?.end()
        return
    }

    activity.obtain(completionHandler: { [weak self] lead to
        guard let continuation = self?.continuation else {
            return
        }

        do {
            let message = strive end result.get()
            continuation.yield(message)
            self?.waitForNextValue()
        } catch {
            continuation.end(throwing: error)
        }
    })
}

With this strategy we don’t have any lingering duties, and our name web site is as clear and concise as ever; we’ve solely modified a few of our inner logic.

In Abstract

Swift Concurrency supplies many helpful options for writing higher code, and Apple shortly adopted async / await for current APIs. Nonetheless, some APIs that will be helpful are lacking, akin to iterating over internet socket messages.

On this put up, you realized use async streams to create an async sequence that emits internet socket messages. You first noticed a completely async / await model that was neat, however had reminiscence and activity lifecycle points. Then, you noticed a model that mixes a callback-based strategy with the async stream.

The result’s a simple solution to iterate over incoming internet socket messages with async / await. If in case you have any questions, feedback, or enhancements for this put up, please do not hesitate to achieve out to me on Twitter.

Related Articles

Social Media Auto Publish Powered By : XYZScripts.com