Coder Social home page Coder Social logo

gediminaszukas / actioncable-client-kotlin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vinted/actioncable-client-kotlin

0.0 0.0 0.0 159 KB

Ruby WebSockets implementation (Action Cable) client library for Kotlin.

License: MIT License

Kotlin 100.00%

actioncable-client-kotlin's Introduction

actioncable-client-kotlin

Release

Ruby Action Cable client library for Kotlin.

About Vinted's fork

This is a fork of original library: https://github.com/hosopy/actioncable-client-kotlin

Main changes compared to original version:

  • Introduced stable version of Kotlin coroutines
  • Updated okhttp 2.x ---> okhttp 3.x
  • Changed serializer klaxon ---> gson

Usage

Requirements

Download

repositories {
    jcenter()
    maven { url "https://jitpack.io" }
}

dependencies {
    implementation 'com.vinted:actioncable-client-kotlin:0.2.1'
}

Basic

// 1. Setup
val uri = URI("ws://cable.example.com")
val consumer = ActionCable.createConsumer(uri)

// 2. Create subscription
val appearanceChannel = Channel("AppearanceChannel")
val subscription = consumer.subscriptions.create(appearanceChannel)

subscription.onConnected = {
    // Called when the subscription has been successfully completed
}

subscription.onRejected = {
    // Called when the subscription is rejected by the server
}

subscription.onReceived = { data: Any? ->
    // Called when the subscription receives data from the server
    // Possible types...
    when (data) {
        is Int -> { }
        is Long -> { }
        is BigInteger -> { }
        is String -> { }
        is Double -> { }
        is Boolean -> { }
        is JsonObject -> { }
        is JsonArray<*> -> { }
    }
}

subscription.onDisconnected = {
    // Called when the subscription has been closed
}

subscription.onFailed = { error ->
    // Called when the subscription encounters any error
}

// 3. Establish connection
consumer.connect()

// 4. Perform any action
subscription.perform("away")

// 5. Perform any action with params
subscription.perform("hello", mapOf("name" to "world"))

Passing Parameters to Channel

val chatChannel = Channel("ChatChannel", mapOf("room_id" to 1))

The parameter container is Map<String, Any?> and is converted to JsonObject(Gson) internally. To know what type of value can be passed, please read Gson user guide.

Passing Parameters to Subscription#perform

subscription.perform("send", mapOf(
    "comment" to mapOf(
        "text" to "This is string.",
        "private" to true,
        "images" to arrayOf(
            "http://example.com/image1.jpg",
            "http://example.com/image2.jpg"
        )
    )
))

The parameter container is Map<String, Any?> and is converted to JsonObject(Gson) internally. To know what type of value can be passed, please read Gson user guide.

Options

val uri = URI("ws://cable.example.com")
val options = Consumer.Options()
options.connection.reconnection = true

val consumer = ActionCable.createConsumer(uri, options)

Below is a list of available options.

  • sslContext

    options.connection.sslContext = yourSSLContextInstance
  • hostnameVerifier

    options.connection.hostnameVerifier = yourHostnameVerifier
  • query

    options.connection.query = mapOf("user_id" to "1")
  • headers

    options.connection.headers = mapOf("X-Foo" to "Bar")
  • reconnection

    • If reconnection is true, the client attempts to reconnect to the server when underlying connection is stale.
    • Default is false.
    options.connection.reconnection = false
  • reconnectionMaxAttempts

    • The maximum number of attempts to reconnect.
    • Default is 30.
    options.connection.reconnectionMaxAttempts = 30
  • okHttpClientFactory

    • Factory instance to create your own OkHttpClient.
    • If okHttpClientFactory is not set, just create OkHttpClient by OkHttpClient().
    options.connection.okHttpClientFactory = {
        OkHttpClient().also {
            it.networkInterceptors().add(StethoInterceptor())
        }
    }

Authentication

How to authenticate a request depends on the architecture you choose.

Authenticate by HTTP Header

val options = Consumer.Options()
options.connection.headers = mapOf("Authorization" to "Bearer xxxxxxxxxxx")

val consumer = ActionCable.createConsumer(uri, options)

Authenticate by Query Params

val options = Consumer.Options()
options.connection.query = mapOf("access_token" to "xxxxxxxxxxx")

val consumer = ActionCable.createConsumer(uri, options)

License

MIT License

Copyright (c) 2019 Vinted UAB

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

actioncable-client-kotlin's People

Contributors

gediminaszukas avatar

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.