Hi everyone, I am trying to make real time clock u...
# ktor
b
Hi everyone, I am trying to make real time clock using Ktor, websocket and Postman. When I refresh the page, time is automatically updated because of the http get request. Instead of http request, I want to use websocket in order to get the updated value.. Is there any way to do this when I connect to Postman at the same time the time will be updated? I hope I am clear to explain. I am also sending the screenshots of what I did. I highly appreciate for any help!
e
What do you think your current websocket implementation does?
b
my current websocket send the the default value which is "you are connected " and "you said :$receiveText on Postman
h
So what do you want? Do you want to get a push from the server every second via sockets? Or do you want to send a tick via socket every time you get an HTTP request?
a
I think you don't need the get endpoint only the websocket and send in the socket every x seconds the updated timestamp. In the client side you need to create a web that in the load create the connection with the websocket and update the UI with the received value. Also listen to all the updates in the socket for updating the UI state
b
I want to get a push from the server every second via sockets. I do NOT want anything with HTTP request
h
Didn't test it but just create a ticker and call
send
everytime:
Copy code
webSocket("/time") {
  flow {
    while(true) { delay(1.seconds); emit(Unit) }
  }.collect {
    send("Hello")
  }
}
b
This is updating every second in Postman. However it does not update on the client side(webpage) it is still the same hour and second
h
How do you fetch the "clock" from your webpage?
b
ask.png
Where am I doing wrong/>
?
h
Do you use a Ktor/Js websocket connection for your webpage too or do you simple open your browser and open
localhost/time
? The latter is a simple HTTP get request. If you want to get a new value every second in your browser too, you need to use a websocket connection too (ignoring HTTP/2).
b
I do not use Ktor/JS. When I set up the environment I just went to Ktor. I open the browser writing localhost/time.
Should I work on Application.kt file then ?
ask2.png
h
Okay, but this is a simple HTTP get request unable to get updates from the server with Http/1.
b
because the link you sent it to me was written in fun main
I appreciate your help your explanation
h
No, if you want to get updates via websockets in the browser too, you have to create a js "app" to use websockets.
b
where should I create "app.js"
the link you sent it to me everything with extension kt not js???
I am so confused
h
Okay. You have two parts: a backend (server) and a frontend (client), in your case two clients, postman and your browser. You already implemented the server: it contains a websocket endpoint
/time
sending some text every second. Now the clients: postman is a tool supporting a websocket connection after configurating it. So now you need to create a javascript application to configure the request from your browser to the server to use websockets too. At the moment you just send a HTTP get request to your server, which is stateless and does not update by itself. Instead you need to create a js app returned by your server (or some other host), to start the websocket connection. Take a look at https://kotlinlang.org/docs/js-overview.html if you want to implement your web app in Kotlin too.
b
where should app.js located?
the file i have to create in the directory
a
Here is a simple example of how you can implement it.
b
Let me check it. Thanks
When you start to new project, did you only selected Ktor environment or Kotlin multiplatform
I do not have gradle/wrapper folder
a
I selected “Kotlin multiplatform”
The Gradle’s wrapper directory is present in the repository.