Code Monkey home page Code Monkey logo

feathersjsclientswift's Introduction

It was archived. Please write to me at [email protected] in case any issues occur.

FeathersjsClientSwift

CI Status Version License Platform

Description

Feathersjs SocketIO Client for iOS wrote in Swift 3.0

Could receive and emit events(with/without ack), handle server responses using clear response object. Processing auto reconnects, but you should handle Authentification yourself.

Example

To run the example project, clone the repo, and run pod install from the Example directory first. You could use local featherjs server by cloninig this repo https://github.com/truebucha/FeathersjsTestApp

HOWTO use http transport

add to your app info plist

<key>NSAppTransportSecurity</key>
	<dict>
		<key>NSAllowsArbitraryLoads</key>
		<true/>
		<key>NSExceptionDomains</key>
		<dict>
			<key>127.0.0.1</key>
			<dict>
				<key>NSIncludesSubdomains</key>
				<true/>
				<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
				<true/>
				<key>NSTemporaryExceptionMinimumTLSVersion</key>
				<string>TLSv1.1</string>
			</dict>
			<key>featherstest.herokuapp.com</key>
			<dict>
				<key>NSIncludesSubdomains</key>
				<true/>
				<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
				<true/>
				<key>NSTemporaryExceptionMinimumTLSVersion</key>
				<string>TLSv1.1</string>
			</dict>
		</dict>
	</dict>

HOWTO connect

        self.feathers = FeathersClient(URL: URL(string: "https://feathersjs-client-swift.herokuapp.com/")!,
                                       namespace: nil,
                                       token: "sdfsdfsdf",
                                       timeout: 60)
        
        let auth = UserAuth(email: "[email protected]", password: "pass5")
        
        guard auth != nil else {
            print("Email should be valid and password length should be more than 5 symbols")
            return
        }
        
        feathers?.onConnect = { [unowned self] response, ack in
            
            do { try self.feathers?.authorize(auth!) { (response) in
                let error = response.extractError()
                guard error == nil else {
                    print("Authentification error: \r\n \(error)")
                    return
                }
                
                let object = response.extractDataObject()
                let ID = object?["id"]
                let userID = ID as? Int
                
                guard userID != nil else {
                    let reason = NSLocalizedString("INVALID_USER_ID", comment: "")
                    print("Authentification error: \r\n \(reason)")
                    return
                }
                print("Signed in as user with id \(userID)")
                // Do you stuff here
                }
            } catch {
                print("Connection error: \r\n \(error)")
            }
        }
        
        feathers?.onError = { response, ack in
            if case let FeathersResponse.error(error) = response {
                print("Connection error: \r\n \(error)")    
            }
        }
        
        feathers?.onDisconnect = { response, ack in
            print("Connection disconnect")
        }
        
        feathers?.onUnathorize = { response, ack in
            print("Connection unauthorized")
        }
        
        feathers?.connect()

HOWTO send event (create user)


        let object: FeathersRequestObject = ["email": "[email protected]",
                                             "password" : "pass5"]
        let emitter = Emitter(feathers: self.feathers!,
                              event: "users::create",
                              authRequired: false)
        
        do { try emitter.emitWithAck(object) { (response) in
            let error = response.extractError()
            guard error == nil else {
                print("Creation error error: \r\n \(error)")
                return
            }
            let object = response.extractResponseObject()
            print("Received \(object)")
            // you stuff
            }
        } catch {
            print("Connection error: \r\n \(error)") 
        }

HOWTO receive event (unauthorization event)

        authFailedReceiver =  Receiver(feathers: self.feathers!,
                                       event: "unauthorized")
        do { try
            self.authFailedReceiver?.startListening() { (response, ack) in
            let object = response.extractResponseObject()
                print("Received unauthentification event \(object)")
            }
        } catch {
            print("===== authFailedReceiver has error: \(error)")
        }


Requirements

Installation

FeathersjsClientSwift is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "FeathersjsClientSwift"

Author

Kanstantsin Bucha, [email protected]

License

FeathersjsClientSwift is available under the MIT license. See the LICENSE file for more info.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.