Giter VIP home page Giter VIP logo

livegql's Introduction

LiveGQL: Use GraphQL Websocket subscription in Swift

Carthage compatible CocoaPod GraphQL

LiveGQL is a simple library to use GraphQL Subscribtion on WebSocket based on Apollo Protocol.

The Android version is here

Features

  • Connect to a GraphQL WebSocket server
  • Send messages
  • Subscribe / unsubscribe
  • Close connection
  • Data handling (delegate)
  • Error handling
  • Reconnect option
  • JSON raw response
  • Queue of unsent messages
  • Implement all Apollo protocol (today just Client part totally implemented)
  • Next: Swift 4 update with native JSON Encoder/Decoder

Requirements

  • iOS 9.0 / tvOS 9.0
  • Xcode >= 8.1
  • Swift >= 3.0

We also use Starscream and JSONCodable, thanks to them

Communication

  • Use issue if you have any problem
  • Don't hesitate to contribute to the project with a pull request

Installation

CocoaPods

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'LiveGQL', '~> 1.1.1'
end

Carthage

github "florianmari/LiveGQL"

See Carthage guide for more informations about integrating with Carthage.

Manually

Just copy files in the Source folder!

Usage

Initializing

Important: to not fall out the variable in the scope, please instantiate above your viewDidLoad() for example.

import LiveGQL

let gql = LiveGQL(socket: "ws://localhost:7003/feedback")

gql.delegate = self
gql.initServer(connectionParams: nil, reconnect: true)

You can set a Dictionnary[String:String] as connectionParams like for authentification by example.

Subscribe / Unsubscribe

Subscribe

Just call subscribe method, set an identifier and your subscription query as well.

gql.subscribe(graphql: "subscription {feedbackAdded {id, text}}", identifier: "feed")
Treat server response

You have to implement delegate method, in your main ViewController (for example) just att that

override func viewDidLoad() {
        super.viewDidLoad()
        gql.delegate = self
        // Do any additional setup after loading the view, typically from a nib.
    }

Below your class add the folowing extension and implement the method:

extension ViewController: LiveGQLDelegate {
    func receivedMessage(text: String) {
        print("Received Message: \(text)")
    }
}

Unsubscribe

Just call unsubscribe method and your identifier

gql.unsubscribe(identifier: "feed")

Close connection

gql.closeConnection()

Bugs

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.