Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

410
Views
How to implement scarlet websocket library with okhttp in Java?

I wanted to know if there is any java implementation of Scarlet web socket library.

 @EchoBotScope
 @Component(modules = [(EchoBotComponent.EchoBotModule::class)], dependencies = [(EchoBotComponent.Dependency::class)])
interface EchoBotComponent {

    fun inject(echoBotFragment: EchoBotFragment)

    interface Dependency {
        fun application(): Application
    }

    @Component.Builder
    interface Builder {
        fun dependency(dependency: Dependency): Builder

        fun build(): EchoBotComponent
    }

    @Module
    class EchoBotModule {
        @Provides
        @EchoBotScope
        fun provideOkHttpClient(): OkHttpClient = OkHttpClient.Builder()
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC))
            .build()

        @Provides
        @EchoBotScope
        fun provideLifecycle(application: Application, loggedInLifecycle: LoggedInLifecycle): Lifecycle =
            AndroidLifecycle.ofApplicationForeground(application)
                .combineWith(loggedInLifecycle)

        @Provides
        @EchoBotScope
        fun provideEchoService(client: OkHttpClient, lifecycle: Lifecycle): EchoService {
            val scarlet = Scarlet.Builder()
                .webSocketFactory(client.newWebSocketFactory("wss://demos.kaazing.com/echo"))
                .lifecycle(lifecycle)
                .addMessageAdapterFactory(BitmapMessageAdapter.Factory())
                .addStreamAdapterFactory(RxJava2StreamAdapterFactory())
                .build()
            return scarlet.create()
        }
    }

    interface ComponentProvider {
        val echoBotComponent: EchoBotComponent
    }
}

How can customize the demo app to make my own okhttp WebSocket client?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I am assuming here that you want to use scarlet in a java project. First of all, the version you are using is outdated. Please find the latest version of scarlet library here. It has breaking changes and you will need to migrate your code.

You can use the scarlet library in a java project as is without depending on the kotlin specific modules likescarlet-stream-adapter-coroutines. An example EchoBotModule implementation in java is given below.

  @Module
class EchoBotModule {
    @Provides
    @EchoBotScope
    OkHttpClient provideOkHttpClient() {
        return OkHttpClient.Builder()
                .addInterceptor(HttpLoggingInterceptor()
                        .setLevel(HttpLoggingInterceptor.Level.BASIC))
                .build();
    }


    @Provides
    @EchoBotScope
    Lifecycle provideLifecycle(Application application, LoggedInLifecycle loggedInLifecycle) {
        return AndroidLifecycle.ofApplicationForeground(application)
                .combineWith(loggedInLifecycle);
    }

    @Provides
    @EchoBotScope
    EchoService provideEchoService(OkHttpClient client, Lifecycle lifecycle) {
        Protocol pr = new OkHttpWebSocket(client, new OkHttpWebSocket.SimpleRequestFactory(
                () -> new Request.Builder().url("wss://demos.kaazing.com/echo").build(),
                () -> ShutdownReason.GRACEFUL
        ));

        List<MessageAdapter.Factory> messageAdapterFactories = Collections.singletonList(new BitmapMessageAdapter.Factory());

        List<StreamAdapter.Factory> streamAdapterFactories = Collections.singletonList(new RxJava2StreamAdapterFactory());

        Scarlet.Configuration configuration = new Scarlet.Configuration(lifecycle, null, streamAdapterFactories, messageAdapterFactories, false);

        Scarlet scarlet = new Scarlet(pr, configuration);
        return scarlet.create();
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!