Websocket client in Kotlin - kotlin

I was wondering why there is no Websocket client native object in Kotlin like it is in javascript.
How does one connect to a Websocket server in kotlin as I created a Node.js websocket server and want to connect to it using an Android client.
Can someone please elaborate on this.
Thanks.

There is. If your question is about Kotlin native/multiplatform, you can use ktor client:
https://ktor.io/clients/websockets.html.
On the Android side, you would need to use CIO or OKHttp engines. On the javascript side, Js engine. On the iOS side, there is currently no support available out of the box, but it's supposed to be coming soon.
If your question is not about Kotlin native/multiplaform, you can still use the above or org.java_websocket.client.WebSocketClient, or any of the third party libs.

Related

Twilio Chat - can we use the npm package `twilio-chat` on server side?

I am trying an approach to encapsulate Twilio to our back end and using our own web socket for real-time chat experience.
Can I use twilio-chat behind my web socket as a proxy server?
Twilio developer evangelist here.
twilio-chat is built as a browser based SDK and consequently relies on browser platform APIs, most notably web sockets. Node.js does not have a native or standard library implementation of web sockets, so you would have to polyfill them and likely other APIs into the global scope.
What I'm saying is that it's likely possible, but probably a hassle.

Custom protocol on top of ktor that is not HTTP or websockets with native support?

I am writing a server/client application with its own custom protocol. It is asynchronous which fits with Kotlin and Ktor.
I wonder how I can use Ktor to implement my own protocol on top on ktor-server-cio engine with full support for kotlin/Native.
Where could I find documentation or examples, I can't find it in the documentation, also it is hard to see what is and is not dependent of kotlin/JVM?
Thankyou in advance.
The CIO engine for a server doesn't support Kotlin/Native yet.
You can use sockets API from the ktor-network module, which does support Kotlin/Native, to implement client and server for your custom protocol on top of TCP or UDP.
As an example, take a look at rsocket-kotlin library where transports are implemented based on Ktor.

http requests with Kotlin multiplatform for desktop

I'm new to kotlin multiplatform and I'm using compose for desktop, how can I make HTTP requests?
is there any resources to learn from?
Ktor is the best supported multiplatform HTTP client currently, as it is maintained by Jetbrains There are also projects that put together many open source tools for kotlin multiplatform such as: AAkira
There is not a specific place currently that goes over HTTP communication, as it would be specific per tool you choose.

Does Mobilefirst provide a provision to access web services directly?

I am developing a native android app on MobileFirst platform. Does MobileFirst provide any code to connect to a web service instead of going through adapters? This is basically only for Native Android development and not for Hybrid app.
The MobileFirst SDK only provides what is required to work with features provided by MobileFirst, such as connecting to various backends using Adapters with the added benefit of the MobileFirst security framework, and other features.
If you have a need to connect to backends not via MobileFirst, use other common client-side utilities to achieve that.
I used the following code and it worked.
WLResourceRequest request = new WLResourceRequest("Actual server path here", GET);
request.addHeader(new BasicHeader("IfAnyHeader", "here"));
request.send(new ResponseListener());
If you are using native Android, you could use volley (or similar) to call external web services or REST apis.
http://developer.android.com/training/volley/index.html
Nothing to do with MobileFirst though, all totally standard Android native coding.

Can I use a MobileFirst app without the MobileFirst server?

Can I consume a web service in a MobileFirst application without a MobileFirst server?
Yes, you can use your MobileFirst app without the need to connect to a MobileFirst server.
The drawback of not using the MobileFirst server is that you'll be loosing all of the features it provides like authentication, security, adapters, unified push notifications, direct update (for hybrid), remote disable, and other features.
If you want to make a request to any endpoint you can use WLResourceRequest (available from version 7.0 onwards) or any other native method to make HTTP requests.
Information on how to use WLResourceRequest
Android:
https://www-01.ibm.com/support/knowledgecenter/SSHS8R_7.0.0/com.ibm.worklight.apiref.doc/html/refjava-worklight-android-native/html/com/worklight/wlclient/api/WLResourceRequest.html
Hybrid:
https://www-01.ibm.com/support/knowledgecenter/SSHS8R_7.0.0/com.ibm.worklight.apiref.doc/html/refjavascript-client/html/WLResourceRequest.html?cp=SSHS8R_7.0.0%2F9-0-0-1-31
iOS: https://www-01.ibm.com/support/knowledgecenter/SSHS8R_7.0.0/com.ibm.worklight.apiref.doc/html/refobjc-worklight-ios/html/interface_w_l_resource_request.html%23a004749b662c6f4a55a3b76e47f7e6062?lang=en
If the call is for example to an external resource you can use the same plain regular AJAX calls as you would anywhere else.
If the resource is protected by MobileFirst Platform, then you must use adapters. Adapters must go through the MobileFirst Server.
Use it like simple IDE to develop application(Native and hybrid).You can use client side API also that do not connect to the mobilefirst server.
You cannot use a mobile first app if you are using MF 7.0 or higher without a server, as soon as you launch the app it invokes an authorization request something like following:
<< domain >>/<< context >>/authorization/v1/clients/instance
Which connects to your workflight server and if it doesnt get response it will fail.
As far as invoking a web service is concerned that is just javascript if you are doing hybrid you can use AJAX as mentioned in another answer. If you are building native IOS or Android you can invoke http request using sdk libraries.
Cheers !