Skip to content

loopsocial/JavaPhoenixClient

Repository files navigation

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 and GSON into the Socket to provide your own configuration

// Configure your own GSON instance
val gson = Gson.Builder().create()

// 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,
                     gson,
                     client)

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:0.5.0'
}

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.

About

Phoenix client for the JVM built with Kotlin

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • Kotlin 99.8%
  • Shell 0.2%