A Brand New Feathers Swift Client

Brendan Conron
The Feathers Flightpath
3 min readMay 15, 2017

--

If you’re like me, you want your dev work to be simple. The more that can be abstracted away into some far off place where you don’t have to think about it the better. That’s why I use FeathersJS, an awesome web framework with some serious capabilities. Need to spin up a REST API? One line of code. Need to add socket/real-time support? One line of code. Oh you’re using NeDB (or PostgresSQL, MongoDB, or Redis? Seriously, take a look at how many databases FeathersJS supports). FeathersJS makes building backends fun, the way it should be.

So what’s the problem? If you’re using FeatherJS on the backend and developing a native iOS application, you’re constrained to using NSURLSession or the ever popular Alamofire to do your networking. Interacting with FeathersJS isn’t easy anymore, it’s a pain. There’s no easy connection, you have to build every endpoint yourself, and service hooks? Forget about it. At the end of the day, those solutions just don’t cut it. FeathersJS is missing something, an iOS mobile client.

After many late nights of hard work, I’m proud to announce the release of FeathersSwift, a fully Swift 3.0 compatible SDK for connecting your native iOS applications to a FeathersJS backend.

This small, un-opinionated framework packs quite the punch, providing REST and SocketIO support out of the box. Gone are the days of spinning up your own clients or messing around with HTTP requests. FeathersSwift takes that all away from you, providing a single, elegant, and type-safe API that you’ve come to know and love from FeathersJS.

So how do you do it? How easy is it really?

let feathersRestApp = Feathers(provider: RestProvider(baseURL: URL(string: "http://myfeathersserver.com")!))let beforeHooks = Service.Hooks(all: [RequestLoggerHook()])
let errorHooks = Service.Hooks(all: [ReauthenticationHook()])
let userService = feathersRestApp.service(path: "users")
userService.hooks(before: beforeHooks, error: errorHooks)
feathersRestApp.authenticate(["strategy": "local":, "username": "my_username", "password": "some_password"]) { token, error in
// do something with the service
userService.request(.find(parameters: ["name": "Bob"]) { error, response in
print(response)
}
}

Those simple lines of code are all it takes to set up a Feathers client that:

  • Re-authenticates once a request comes back with an unauthenticated error.
  • Logs every request.
  • Easy querying API.

There’s also a SocketIO provider:

let feathersSocket = Feathers(provider: SocketProvider(baseURL: URL(string: "http://myfeathersserver.com")!, configuration: []))feathersSocket.on(.created) { object in 
// Listen to all real-time events for objects that are created
}

What’s that you say? Real-time event observation out of the box? That’s right, and so much more!

To find out more, visit the Github page for the project and check out the README. If that’s not enough, check out the code! Every part of the framework is well-documented and the framework is designed to be easy to understand by default and matching feature parity with the JS libraries. Finally, if you have any questions, find a bug, or want to contribute, open an issue, make a PR, or feel free to ping me on the FeathersJS slack channel, username: brendan.

Thanks for reading and I hope you enjoy using FeathersSwift! I think you’ll find it more than lives up to the awesomeness of the FeathersJS family.

Cheers 🍻

--

--

iOS @DoorDash. @tuftsuniversity. @feathersjs. Sometimes sleeping, always coding.