Confuse Library Server Sent Event for Android Client Nginx Push Stream - android

Our server is used to Nginx as webserver and add compile module of nginx_push_stream. Before used to push stream had used to Restful then changed to Websocket but WebSocket sometimes lost when the client or server had small bandwidth. In the 2019 year, from Websocekt to Server-Sent Event (SSE) / event-source such as event stream or text/event-stream to reduce loss both of client or server.
Please, anyone, have any idea for library event stream is able to use to the android client and iPhone client.
I have already used to Okhttp but there is not ready yet used event stream, RxSSE is not able to use in Android no response at all.
I hope that next year OkHttp is already updated OkHttp-EventSource for Android Client also iPhone Client

After 3 days, Struggling had search library for supporting SSE of Android client. Then, i found this blog Accessing SSE help me a lot to implementation SSE, also this the library SSE
This sample implementation SSE in kotlin version, even thought library is java version.
1. Preparing for event handler source
interface DefaultEventHandler : EventHandler {
#Throws(Exception::class)
override fun onOpen() {
Log.i("open","open")
}
#Throws(Exception::class)
override fun onClosed() {
Log.i("close","close")
}
#Throws(Exception::class)
override fun onMessage(event: String, messageEvent: MessageEvent) {
Log.i("event", messageEvent.data)
}
override fun onError(t: Throwable) {
Log.e("error", t.toString())
}
override fun onComment(comment: String) {
Log.i("event", comment)
}
}
class MessageEventHandler : DefaultEventHandler {
override fun onMessage(event: String, messageEvent: MessageEvent) {
super.onMessage(event, messageEvent)
val data = messageEvent.data
Log.i("message", data)
}
}
2. implementation event source
import java.net.URI
import java.util.concurrent.TimeUnit
.....
fun initEventSource(url: String) {
val eventHandler = MessageEventHandler()
try {
val eventSource: EventSource = EventSource.Builder(handler, URI.create(url))
.reconnectTimeMs(3000)
.build()
eventSource.start()
TimeUnit.SECONDS.sleep(10)
} catch (e: Exception) {
Log.e("error", e.toString())
}
}
I hope this would be alternative method protocol from client to server than used RESTfull or Websocket. When server always sent data to client without need request from client as stream.
I have added a gist of using library SSE https://gist.github.com/subhanshuja/9079ec9df0927b1da26ee57cf9da1f26.

Related

Android Kotlin Coroutine Channel message not sended in websocket callback

I am developing an chat android app using WebSocket (OkHttp)
To do this, I implemented the okhttp3.WebSocketListener interface.
And I am receiving the chat messages from the onMessage callback method.
I already developed it using the Rx-PublishSubject, and it works fine.
But I want to change it to Coroutine-Channel.
To do this, I added the Channel in my WebSocketListener class.
#Singleton
class MyWebSocketService #Inject constructor(
private val ioDispatcher: CoroutineDispatcher
): WebSocketListener() {
// previous
val messageSubject: PublishSubject<WsMsg> = PublishSubject.create()
// new
val messageChannel: Channel<WsMsg> by lazy { Channel() }
override fun onMessage(webSocket: WebSocket, text: String) {
super.onMessage(webSocket, text)
// previous
messageSubject.onNext(text)
// new
runBlocking(ioDispatcher) {
Log.d(TAG, "message: $text")
messageChannel.send(text)
}
}
}
But... the coroutine channel doesn't work...
It receives and prints the Log only once.
But it doesn't print the log after the second message.
But when I change the code like below, it works!
override fun onMessage(webSocket: WebSocket, text: String) {
super.onMessage(webSocket, text)
GlobalScope.launch(ioDispatcher) {
Log.d(TAG, "message: $text")
messageChannel.send(text)
}
}
The difference is runBlocking vs GlobalScope.
I head that the GlobakScope may not ensure the message's ordering.
So It is not suitable for the Chat app.
How can I solve this issue?
The default Channel() has no buffer, which causes send(message) to suspend until a consumer of the channel calls channel.receive() (which is implicitely done in a for(element in channel){} loop)
Since you are using runBlocking, suspending effectively means blocking the current thread. It appears that okhttp will always deliver messages on the same thread, but it can not do that because you are still blocking that thread.
The correct solution to this would be to add a buffer to your channel. If it is unlikely that messages will pour in faster than you can process them, you can simply replace Channel() with Channel(Channel.UNLIMITED)

Android WorkManager - Can I pass input data to a Worker that runs periodically?

In my app I start a WebSocketWorker tasks that runs periodically every 15 minutes. As the name implies, it contains a WebSocket for listening to a socket in the background:
// MainApplication.kt
override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
Timber.plant(DebugTree())
}
val work = PeriodicWorkRequestBuilder<WebSocketWorker>(15, TimeUnit.MINUTES).build()
workManager.enqueueUniquePeriodicWork("UniqueWebSocketWorker", ExistingPeriodicWorkPolicy.KEEP, work)
}
The WebSocketWorker contains the following logic:
#HiltWorker
class WebSocketWorker #AssistedInject constructor(
#Assisted appContext: Context,
#Assisted workerParams: WorkerParameters
) : CoroutineWorker(appContext, workerParams) {
inner class MyWebSocketListener : WebSocketListener() {
override fun onMessage(webSocket: WebSocket, text: String) {
Timber.d("The message sent is %s", text)
// do sth. with the message
}
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
t.localizedMessage?.let { Timber.e("onFailure: %s", it) }
response?.message?.let { Timber.e("onFailure: %s", it) }
}
}
override suspend fun doWork(): Result {
try{
// code to be executed
val request = Request.Builder().url("ws://***.***.**.***:8000/ws/chat/lobby/").build()
val myWebSocketListener = MyWebSocketListener()
val client = OkHttpClient()
client.newWebSocket(request, myWebSocketListener)
return Result.success()
}
catch (throwable:Throwable){
Timber.e("There is a failure")
Timber.e("throwable.localizedMessage: %s", throwable.localizedMessage)
// clean up and log
return Result.failure()
}
}
}
As you can see, in the Worker class I set the WebSocket and everything is fine. Listening to the socket works.
Now, I also want to add the "sending of messages" functionality to my app. How can I reuse the websocket created in WebSocketWorker? Can I pass input data to the WebSocketWorker that runs in the background ?
Let's say I have a EditText for typing the message and a Button to send the message with a setOnClickListener attached like this:
binding.sendButton.setOnClickListener {
// get message
val message = binding.chatMessageEditText.text.toString()
// check if not empty
if(message.isNotEmpty()) {
// HOW CAN I REUSE THE WEBSOCKET RUNNING PERIODICALLY IN THE BACKGROUND?
// CAN I PASS THE MESSAGE TO THAT WEBSOCKET ?
// OR SHOULD I CREATE A DIFFERENT WORKER FOR SENDING MESSAGES (e.g.: a OneTimeRequest<SendMessageWorker> for sending messages ?
}
}
From the documentation, I know that you need to build Data objects for passing inputs and so on but there was no example which showcased how to pass input to a worker running periodically in the background.
My experience is saying that you can. Basically you "can't" interact with the worker object via the API. It is really annoying.
For example, with the JS you have the option to get a job and check the parameters of the job. There is no such option with the work. For example, I want to check what is the current state of the restrictions - what is satisfied, what is not. Nothing like this. You can just check states, cancel and that is almost all.
My suggestions is that it is because the WorkManager is a "facade/adapter" over other libraries like JS. It has it's own DB to restore JS jobs on device restart and stuff like this, but beside that if you want to interact with the internals I guess it was just too complicated for them to do so they just skipped.
You can just inject some other object and every time the work can ask it for it's data. I don't see other option.

How can Retrofit handle invalid responses from interceptor?

I've spent hours trying to figure this thing out, and I still can figure it out.
I'm trying to retrieve data from a website using JSON.
If the website is live and everything, it works, but if the website returns something else than the data, like a 403 error, or any other error, then it crashes. I tried to debug it, but I still don't understand what is going on here.
Here is my code:
I have a NetworkModule with an interceptor that is supposed to check is the response is valid or not, and from what I can tell it works, because my variable isDataRetrievable is false (the value by default):
val networkModule = module {
single {
val customGson =
GsonBuilder().registerTypeAdapter(Lesson::class.java, LessonDeserializer())
.create()
Retrofit.Builder()
.client(get())
.addConverterFactory(
GsonConverterFactory.create(customGson)
)
.baseUrl(BuildConfig.URL)
.build()
}
factory {
OkHttpClient.Builder()
.addInterceptor(Interceptor { chain ->
chain.withConnectTimeout(1,TimeUnit.SECONDS)
val request: Request = chain.request()
val response = chain.proceed(request)
if (response.isSuccessful){
networkStatus.isDataRetrievable = true
}
response
}).build()
}
factory {
get<Retrofit>().create(LessonApi::class.java)
}
}
Next, I have my API to get the data:
interface LessonApi {
#GET("/JSON/json_get_data.php")
suspend fun getLessons(): Call<Lesson>
}
Then, for some reason, I have a repository (I'm not the only one working on this code, I didn't do this part):
class LessonRepository(private val service: LessonApi) {
suspend fun getLessons() = service.getLessons()
}
Then, I have my splash screen view model, that is supposed to retrieve the data if possible:
if (networkStatus.isNetworkConnected && networkStatus.isWebsiteReachable) {
var tmp = repository.getLessons()
tmp.enqueue(object : Callback<Lesson> {
override fun onFailure(call: Call<Lesson>, t: Throwable) {
Log.d("DataFailure",t.message.toString())
nextScreenLiveData.postValue(false)
}
override fun onResponse(call: Call<Lesson>, response: Response<Lesson>) {
Log.d("DataFailure","Test")
}
})
}else{
nextScreenLiveData.postValue(false)
}
The problem is that when the program get to the line repository.getLessons(), it crashes with the error:
retrofit2.HttpException: HTTP 403
at retrofit2.KotlinExtensions$await$2$2.onResponse(KotlinExtensions.kt:49)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:129)
at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
So onFailure or onResponse are never called. I tried to run the debugger, to step in, but I cannot figure it out when it fails.
I thought it was because it was trying to deserialize invalid data, but I put breakpoints everywhere in my deserializer, and it never hits the breakpoints.
I'm not a professional android developer, but I'm very confused here.
What I'd like to do is that if the request is unsuccessful, just discard the response (do not deserialize it), and display a message or exit.
Please help, it's so frustrating. I'm not sure how to intercept errors or what to do if Interceptors get an unsuccessful request (for now I just set a variable but it's unused).
Thanks.
Edit: What I'm trying to do, is to retrieve data from a webserver. If it cannot (for any reason), I don't want the gson to parse data (because it will probably be garbage and will not correspond to my deserializer). However, I feel like this okhttp / retrofit is a pipeline, where okhttp get the response from the webserver and pass it to a gson converter. What I want to do is intercept this response, and if it's not successful, to NOT pass it to gson, set a variable, so that the rest of my application knows what to do. But the thing is, for now, it just crash even before it gets to the callback in enqueue. The interceptor works just fine, except I'd like him to drop the response if it's not successful. Is it possible?
I tried something like that, and it worked to handle bad codes (>400), but I also wanted to handle malformed JSON data, so I added the onResponse and onFailure callbacks, but it never worked, because when I receive a malformed JSON, it also trigger an exception, and then go in the catch before it can go on the 'enqueue', so I'm not sure what this is used for.
try {
val lessons = repository.getLessons().enqueue(object : Callback<List<Lesson>> {
override fun onResponse(call: Call<List<Lesson>>, response: Response<List<Lesson>>) {
networkStatus.isDataRetrievable = response.isSuccessful
Log.d("Retrofit", "Successful response")
nextScreenLiveData.postValue(response.isSuccessful)
}
override fun onFailure(call: Call<List<Lesson>>, t: Throwable) {
Log.d("Retrofit", "Failure response")
nextScreenLiveData.postValue(false)
}
})
nextScreenLiveData.postValue(true)
} catch (e: Exception) {
nextScreenLiveData.postValue(false)
}
Anyway, just this code works for everything in the end:
try {
val lessons = repository.getLessons().filter {
it.lesson.contains("video")
}.filter {
DataUtils.isANumber(it.id)
}
lessonDao.insertLessons(lessons)
networkStatus.isDataRetrievable = true
} catch (e: Exception) {
networkStatus.isDataRetrievable = false
}
But in my API, I don't return callbacks, I directly return the objects, as such:
#GET("/JSON/json_get_dat.php")
suspend fun getLessons(): List<Lesson>
I don't know if this is the right way to do it, but it works. I hope this might help others.

How to keep an Android grpc client with server streaming connection alive?

I have a grpc-js server and a Kotlin for Android client that makes a server streaming call. This is the GRPCService class.
class GRPCService {
private val mChannel = ManagedChannelBuilder
.forAddress(GRPC_HOST_ADDRESS, GRPC_HOST_PORT)
.usePlaintext()
.keepAliveTime(10, TimeUnit.SECONDS)
.keepAliveWithoutCalls(true)
.build()
val asyncStub : ResponderServiceGrpc.ResponderServiceStub =
ResponderServiceGrpc.newStub(mChannel)
}
And the method is called from a foreground service.
override fun onCreate() {
super.onCreate()
...
startForeground(MyNotificationBuilder.SERVICE_NOTIFICATION_ID, notificationBuilder.getServiceNotification())
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val userId = sharedPreferencesManager.getInt(SharedPreferencesManager.USER_ID)
val taskRequest = Responder.TaskRequest.newBuilder()
.setUserId(userId)
.build()
grpcService.asyncStub.getTasks(taskRequest, object :
StreamObserver<Responder.TaskResponse> {
override fun onCompleted() {
Log.d("grpc Tasks", "Completed")
}
override fun onError(t: Throwable?) {
Log.d("grpc error cause", t?.cause.toString())
t?.cause?.printStackTrace()
Log.d("grpc error", "AFTER CAUSE")
t!!.printStackTrace()
}
override fun onNext(value: Responder.TaskResponse?) {
if (value != null) {
when (value.command) {
...
}
}
}
})
return super.onStartCommand(intent, flags, startId)
}
The connection opens and stays open for about a minute of no communication and then fails with the following error.
D/grpc error cause: null
D/grpc error: AFTER CAUSE
io.grpc.StatusRuntimeException: INTERNAL: Internal error
io.grpc.Status.asRuntimeException(Status.java:533)
io.grpc.stub.ClientCalls$StreamObserverToCallListenerAdapter.onClose(ClientCalls.java:460)
io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:426)
io.grpc.internal.ClientCallImpl.access$500(ClientCallImpl.java:66)
io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.close(ClientCallImpl.java:689)
io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.access$900(ClientCallImpl.java:577)
io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInternal(ClientCallImpl.java:751)
io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:740)
io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)
The grpc-js server is created with the following options.
var server = new grpc.Server({
"grpc.http2.min_ping_interval_without_data_ms" : 10000,
"grpc.keepalive_permit_without_calls" : true,
"grpc.http2.min_time_between_pings_ms" : 10000,
"grpc.keepalive_time_ms" : 10000,
"grpc.http2.max_pings_without_data" : 0,
'grpc.http2.min_ping_interval_without_data_ms': 5000
});
I never received the too many pings error either.
I noticed that if there is periodic communication (like the server pinging the client with a small amount of data every 30s or so) through this connection then I don't get the error and the connection stays open for as long as the pinging continues (tested for 2 days).
How do I keep the connection open without resorting to periodically pinging the client?
The managed channel has a property called keepAliveWithoutCalls which has a default value of false as seen here. If this is not set to true then the keepAlive will not happen if there are no current active calls happening. You would need to set this like so:
private val mChannel = ManagedChannelBuilder
.forAddress(GRPC_HOST_ADDRESS, GRPC_HOST_PORT)
.usePlaintext()
.keepAliveTime(30, TimeUnit.SECONDS)
.keepAliveWithoutCalls(true)
.build()
There is a possibility that you will have to do some other settings on the server as well to have the connection stay open without any data passing. You might get an error on the server saying "too many pings". This happens because there are some other settings GRPC needs. I am not sure exactly how to achieve this with a JS server but it shouldn't be too difficult. These settings include:
GRPC_ARG_HTTP2_MIN_RECV_PING_INTERVAL_WITHOUT_DATA_MS
Minimum allowed time between a server receiving successive ping frames without sending any data/header/window_update frame.
And this one:
GRPC_ARG_HTTP2_MIN_SENT_PING_INTERVAL_WITHOUT_DATA_MS
Minimum time between sending successive ping frames without receiving any data/header/window_update frame, Int valued, milliseconds.
And this one:
GRPC_ARG_KEEPALIVE_PERMIT_WITHOUT_CALLS
Is it permissible to send keepalive pings without any outstanding streams.
There is a Keepalive User Guide for gRPC which I suggest you read through to understand how gRPC should keep connections open. This is the core standard that all server and client implementations should follow, but I have noticed this is not always the case. You can have a look at a previous but similar question I asked a while back here.
Have you tried the ManagedChannelBuilder.keepAliveTime setting (https://github.com/grpc/grpc-java/blob/master/api/src/main/java/io/grpc/ManagedChannelBuilder.java#L357) ? I am assuming it will work in the middle of a server streaming call.

Using OkHTTPClient() inside a coroutine always throws warning "inappropriate blocking method called"

What is the correct way to call the OkHTTP client inside a Coroutine?
CoroutineScope(IO).launch {
val request = Request.Builder()
.url("${host}/dots")
.build()
val client = OkHttpClient()
client.newCall(request).enqueue(object: Callback{
override fun onFailure(call: Call, e: IOException) {
isConnected.postValue(false)
}
override fun onResponse(call: Call, response: Response) {
val loadingStr = response.body()?.string().toString()
loadingStrings = loadingStr
Log.i("My_Error",loadingStrings)
}
})
}
In the onResponse the loadingStr variable shows warning for string() saying inappropriate blocking method called. Please tell me the correct way to do the same.
OkHttp provides two modes of concurrency
Synchronous blocking via execute
Asynchronous non-blocking via enqueue
Outside of these most frameworks you use will have bridge methods that convert between different modes and difference frameworks.
You should use a library like https://github.com/gildor/kotlin-coroutines-okhttp to do it for you. This code needs to do the basic normal path but also specifically needs to handle errors and separately cancellation. Your code inside coroutines should never be calling enqueue directly.
suspend fun main() {
// Do call and await() for result from any suspend function
val result = client.newCall(request).await()
println("${result.code()}: ${result.message()}")
}
This is another example from the Coil image loading library which as a framework makes sense to implement this itself rather than using a library
https://github.com/coil-kt/coil/blob/0af5fe016971ba54518a24c709feea3a1fc075eb/coil-base/src/main/java/coil/util/Extensions.kt#L45-L51
internal suspend inline fun Call.await(): Response {
return suspendCancellableCoroutine { continuation ->
val callback = ContinuationCallback(this, continuation)
enqueue(callback)
continuation.invokeOnCancellation(callback)
}
}
https://github.com/coil-kt/coil/blob/a17284794764ed5d0680330bfd8bca722a36bb5e/coil-base/src/main/java/coil/util/ContinuationCallback.kt
OkHttp can't implement this directly for at least two reasons
It would add a dependency Kotlin coroutines library, and require more secondary releases.
This problem isn't specific to Kotlin coroutines, so OkHttp would have code to deal with RxJava 1/2/3, Spring Reactor, KTor etc.

Categories

Resources