Giter Club home page Giter Club logo

javaphoenixclient's Introduction

JavaPhoenixClient

Maven Central Build Status codecov

JavaPhoenixClient is a Kotlin implementation of the phoenix.js client used to manage Phoenix channels.

Basic Usage

fun connectToChatRoom() {

    // Create the Socket
    val params = hashMapOf("token" to "abc123")
    val socket = Socket("http://localhost:4000/socket/websocket", params)

    // Listen to events on the Socket
    socket.logger = { Log.d("TAG", it) }
    socket.onOpen { Log.d("TAG", "Socket Opened") }
    socket.onClose { Log.d("TAG", "Socket Closed") }
    socket.onError { throwable, response -> Log.d(throwable, "TAG", "Socket Error ${response?.code}") }

    socket.connect()

    // Join channels and listen to events
    val chatroom = socket.channel("chatroom:general")
    chatroom.on("new_message") { message ->
        val payload = message.payload
        ...
    }

    chatroom.join()
            .receive("ok") { /* Joined the chatroom */ }
            .receive("error") { /* failed to join the chatroom */ }
}

If you need to provide dynamic parameters that can change between calls to connect(), then you can pass a closure to the constructor

// Create the Socket
var authToken = "abc"
val socket = Socket("http://localhost:4000/socket/websocket", { mapOf("token" to authToken) })

// Connect with query parameters "?token=abc"
socket.connect()


// later in time, connect with query parameters "?token=xyz"
authToken = "xyz"
socket.connect() // or internal reconnect logic kicks in

You can also inject your own OkHttp Client into the Socket to provide your own configuration

// Configure your own OkHttp Client
val client = OkHttpClient.Builder()
    .connectTimeout(1000, TimeUnit.MILLISECONDS)
    .build()

// Create Socket with your custom instances
val params = hashMapOf("token" to "abc123")
val socket = Socket("http://localhost:4000/socket/websocket",
    params,
    client)

By default, the client use GSON to encode and decode JSON. If you prefer to manage this yourself, you can provide custom encode/decode functions in the Socket constructor.

// Configure your own GSON instance
val gson = Gson.Builder().create()
val encoder: EncodeClosure = {
    // Encode a Map into JSON using your custom GSON instance or another JSON library
    // of your choice (Moshi, etc)
}
val decoder: DecodeClosure = {
    // Decode a JSON String into a `Message` object using your custom JSON library 
}

// Create Socket with your custom instances
val params = hashMapOf("token" to "abc123")
val socket = Socket("http://localhost:4000/socket/websocket",
    params,
    encoder,
    decoder)

Installation

JavaPhoenixClient is hosted on MavenCentral. You'll need to make sure you declare mavenCentral() as one of your repositories

repositories {
    mavenCentral()
}

and then add the library. See releases for the latest version

dependencies {
    implementation 'com.github.dsrees:JavaPhoenixClient:1.0.1'
}

Feedback

Please submit in issue if you have any problems or questions! PRs are also welcome.

This library is built to mirror the phoenix.js and SwiftPhoenixClient libraries.

javaphoenixclient's People

Contributors

dsrees avatar dustinconrad avatar ostap0207 avatar adamgrzybkowski avatar alonparker avatar alvarezariel avatar dpreussler avatar eyal-lezmy avatar rawnsley avatar scottymack 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.