joffrey-bion / krossbow

A Kotlin multiplatform coroutine-based STOMP client over websockets, with built-in conversions.
MIT License
196 stars 14 forks source link

Unable to create a sample project following documentation #479

Closed Polve closed 5 months ago

Polve commented 5 months ago

Problem description

Total kotlin and krossbow beginner here, so I probably miss some obvious step.

Anyway, I'm trying to following the getting started guide and I'm even able to understand how to use the sample code:

import kotlinx.coroutines.flow.*
import org.hildan.krossbow.stomp.*
import org.hildan.krossbow.websocket.*
import org.hildan.krossbow.websocket.builtin.*

val client = StompClient(WebSocketClient.builtIn()) // other config can be passed in here
val session: StompSession = client.connect(url) // optional login/passcode can be provided here

session.sendText("/some/destination", "Basic text message") 

// this triggers a SUBSCRIBE frame and returns the flow of messages for the subscription
val subscription: Flow<String> = session.subscribeText("/some/topic/destination")

val collectorJob = launch {
    subscription.collect { msg ->
        println("Received: $msg")
    }
}
delay(3000)
// cancelling the flow collector triggers an UNSUBSCRIBE frame
collectorJob.cancel()

session.disconnect()

Among the problems I have:

What would be very useful for a complete beginner like me it's a source project implementing the getting started guide and maybe a couple sample tests ready.

Is there something like this you can point me at?

Thanks for the great project!

joffrey-bion commented 5 months ago

Hi @Polve, thank you for taking the time to report your issues with the guide. This is very much appreciated.

the ide is unable to resolve the "builtin" package and method

I guess it's an unfortunate choice of examples on my part, sorry. If you only read the code snippets, the first one showing the Gradle dependencies doesn't match the sample code that follows in the guide: the dependencies show the Ktor web socket implementation, while the sample code uses the built-in web socket implementation.

If you intend to use the built-in web socket like in the samples that follow, you should not add the ktor dependency, and instead add the built-in dependency. You can find information on how to add this dependency there: https://joffrey-bion.github.io/krossbow/websocket/builtin/#dependency-information

But basically, use this instead:

implementation("org.hildan.krossbow:krossbow-stomp-core:5.12.0")
implementation("org.hildan.krossbow:krossbow-websocket-builtin:5.12.0")

However, note that the choice of web socket implementation depends on which platforms you want to support. This is why the guide points you to the table of web socket clients, so you can see which ones support which platforms and make your choice. Each web socket client adapter has a corresponding guide to explain what it uses and how to configure it (in particular what dependencies to add to Gradle).

it is also unable to resolve "launch"

I guess I left things out "for brevity" but it backfires in this case. The function launch is a function from the coroutines library, but it's not available as a top-level function like this (it used to be, in the very early days of coroutines). Nowadays, you need to have a CoroutineScope, and call scope.launch { ... }. I should fix the example to at least mention a scope variable to avoid such confusion.

But then you'll have a question about where this scope variable comes from 😄, which leads me to your last point.

(I suppose) the code requires to be used in a coroutine, but as total newbye I would welcome an example

Yes. The entire Krossbow library is based on Kotlin coroutines mechanisms, suspend functions and Flows. Krossbow's documentation assumes existing knowledge of Kotlin and Kotlin coroutines, so if you want to use Krossbow you will have to learn about coroutines first. You can use Krossbow as a way to experiment with coroutines as you learn, of course.

What would be very useful for a complete beginner like me it's a source project implementing the getting started guide and maybe a couple sample tests ready. Is there something like this you can point me at?

That's fair. I have no simple sample project to point you to at the moment, unfortunately, sorry. My main use case for Krossbow is the Seven Wonders game project, which uses Krossbow for its client, but for a beginner it might be a bit overwhelming.

Polve commented 5 months ago

Thanks for the fast and useful reply! My goal is indeed to develop a simple multiplatform client for test project using SpringBoot+websockets-stomp on the backend and the Seven Wonders source is great as a tutorial even if certainly quite overwhelming for me :smile:

I suppose I'll have to use the ktor backend to have full multiplatform compatibilty, and yes, having a minimal sample app that connects to a stomp server, sending some packets and subscribing to a queue would be really an awesome help :-)

What about leaving the issue open so you can put the link here if one day it will be available?

Thanks!

joffrey-bion commented 5 months ago

I suppose I'll have to use the ktor backend to have full multiplatform compatibilty

Sounds about right if you want to use your multiplatform client in the most places, and if the extra Ktor dependencies are OK for you.

What about leaving the issue open so you can put the link here if one day it will be available?

I will use this issue to track the fixes in the getting started guide, and I opened another issue (https://github.com/joffrey-bion/krossbow/issues/480) to track the potential addition of a simple sample project.