Netstrings for Swift

I published another small Swift library: swift-netstring implements reader and writer for D. J. Bernstein's Netstrings format in Swift.

Netstrings is a specification for length-prefixed and delimited byte strings, useful mostly as a low-level building block for network protocols. With swift-netsring you can parse incoming data from a socket or some other stream by wrapping the stream in a simple closure that reads bytes synchronously. Or you can feed it an array of bytes.

You can find examples in the repo in the README, in a Swift playground and in tests. There's also API documentation (a big thank you to Jesse Squires for an excellent guide for setting up docs on GitHub pages for a Swift project.)

Flue: Fluent API for value extraction and conversion for Swift

Flue is a Swift (3.0, as of this writing) library for extracting, validating and converting values from user input. It tries to do this with a fluent interface:

vp.extract("100").maxLength(6).regexp("1.*")!.asInt().required()

It can output readable help and error messages from your conversions. See the README for details.

Enjoy.

Talking to servers: WebSockets

This is a sort of follow-up post to ZeroMQ, iOS and Python from a year ago. I again wrote a test app and server. Of the earlier components, iOS stayed, but ZeroMQ I replaced with WebSockets and the server is this time written in Clojure.

Idea of the exercise is the same as last time: two-way communication between an iOS client and server over a persistent connection. WebSockets is a more mainstream technology than ZeroMQ with a wide variety of servers available, even if it is a young spec and not quite everything has stabilized yet.

On the iOS side there's two options, short of writing the protocol implementation yourself: some kind of horrible Javascript-UIWebView bridge and Square's SocketRocket. I went for SocketRocket. It's available from CocoaPods and at least in this brief test worked fine.

Last time I was planning on doing the server in Clojure but ran out of patience. This time the technology stack was easier. I pretty much followed Jay Fields' example with just a few changes here and there and had a server running in no time.

The system doesn't do anything fancy, or do it particularly beautifully: the app waits for a button tap, sends a number wrapped in JSON to the server, the server increments it and sends it back.

The code is on BitBucket as always. Enjoy.

© Juri Pakaste 2021