WebSocket Library for Kotlin Native?

I am looking for a WebSocket library that I can use in a Kotlin Multiplatform project for my iOS/Native implementation. This project is currently in IntelliJ and I am using Gradle (Kotlin DSL) to manage dependencies.

To give some more context I have the following for my project:

  1. Kotlin Common code for all my common functionality for the project
  2. Platform specific code for my WebSocket implementations, one for for Java, JavaScript and iOS.

Firstly, the reason for the platform specific implementations for WebSockets is because I have not found a common library that supports WebSockets for all platforms that can be written in the common Kotlin code. While Ktor comes close, from what I see in their documentation their WebSocket client does not support iOS (their Http client supports all platforms)

Secondly, there are a bundle of WebSocket Swift/C libraries (Starscream, Socket.io, SocketRocket etc) that can be used in Kotlin Native via CocoaPods in Xcode but since I am writing a Multiplatform project in IntelliJ with Gradle, there is no way to include these libraries in my project dependencies. I’ve also tried using the Gradle Source Dependency option, but IntelliJ cannot build the Swift/C source from Git so this is also not an option.

I am open to any suggestions at this point as I am really trying to avoid the route of writing a whole Swift client library for my project, as the reason we started with Kotlin Multiplatform was to avoid supporting the multitude of client libraries we have to write and support for our clients.

If anyone has any recommendations that would be really helpful.

Here you go :slight_smile:

Keep in mind that Socket.io is not a proper websocket. Basically it’s a library that does exactly the same things of a websocket but does not follow its standards.

2 Likes

@lamba92 Thank you for your response. From what I see in the Ktor documentation they mention “Ktor provides a WebSocket client for the following engines: CIO, OkHttp, Js.” Do they also have support for iOS/Native?

Try using the CIO engine, but idk if it is available for Native as well, you may try!

Thank you! I’m going to try that as an option.

You can even dare to create a Ktor Client Engine interface using an already existing Swift/Objective-C engine. Have a look at:

If you do try I encourage you to publish that engine on Github - write me if you need help publishing it!
Good luck! And please let us know if the CIO engine is available for Native target iOS.

Hey! Just an update, the CIO engine for Ktor is currently not available for Native unfortunately. I installed the dependency into my project but could not import the library into any of my classes targeting the iOS platform. I could however do this for JS and Java and could utilise the library. I have seen comments that Ktor are working on creating a WebSocket client for iOS but it may be months away.

Thanks for your suggestion above! That’s definitely a viable option for me actually, but I’ll need to pass it by my team since it’s production code. I’m going to add this to my ‘list’ of interim/workaround/slightly hacky solutions. (haha)

I’ve also joined the Kotlinlang slack channel, so I’ll post some questions there to see if they have any ideas on this one. If not, my other and last interim/workaround/hacky option is to pull all my code for my Multiplatform project into Xcode and create a Kotlin/Native project from it. Which means maintaining two exact code bases, one for Native/iOS and the other for JavaScript and Java (I hate saying that) until Ktor hopefully comes to the rescue.

1 Like