Skip to content

A Simple Retrofit Inspired Android Websocket Client (In Development)

License

Notifications You must be signed in to change notification settings

ibrahimsn98/achilles

Repository files navigation

Achilles

A Simple Retrofit, Scarlet Inspired Android Websocket Client

API Android Arsenal Android CI

Motivation

I have created this library to communicate the apps with their own backends. All receiver and send methods require to specify event name, so both client and server applications distinguish the right payload.

Payload

  • Data sent
{"event": "echo", "data": {"name": "Mark", "surname": "Bond"}}
  • Data sent (Encoded payload)
{"event": "echo", "data": "eyJuYW1lIjogIk1hcmsiLCAic3VybmFtZSI6ICJCb25kIn0="}
  • Data received
{"event": "echo", "data": {"name": "Mark", "surname": "Bond"}}

Usage

  • Create service interface
interface SocketService {

    @SendEvent("echo")
    fun sendEcho(
        @Field("name") name: String,
        @Field("surname") surname: String
    )

    @ReceiveEvent("echo")
    suspend fun receiveEcho(): Flow<Response>
}
  • Use Achilles to create an implementation:
val achilles = Achilles.Builder()
    .baseUrl("wss://echo.websocket.org")
    .client(OkHttpClient().newBuilder().build())
    .encodePayload(true)
    .logTraffic(true)
    .build()

val service = achilles.create(SocketService::class.java)
  • Send and observe socket event data
CoroutineScope(IO).launch {
    service.receiveEcho().onEach {
        Log.d("MainActivity", "Response: $it")
    }

    service.sendEcho("Name", "Surname")
}

Setup

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

dependencies {
    implementation 'com.github.ibrahimsn98:achilles:1.4'
}

TODO

  • Lifecycle aware connection
  • Reconnect on disconnection
  • Socket status events
  • Payload encryption

License

MIT License

Copyright (c) 2019 İbrahim Süren

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.