https://kotlinlang.org logo
#ktor
Title
# ktor
b

Begum Turan

01/16/2023, 8:18 PM
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

Emil Kantis

01/16/2023, 8:57 PM
What do you think your current websocket implementation does?
b

Begum Turan

01/16/2023, 9:14 PM
my current websocket send the the default value which is "you are connected " and "you said :$receiveText on Postman
h

hfhbd

01/16/2023, 9:17 PM
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

adryan_ip

01/16/2023, 9:35 PM
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

Begum Turan

01/16/2023, 9:54 PM
I want to get a push from the server every second via sockets. I do NOT want anything with HTTP request
h

hfhbd

01/16/2023, 9:57 PM
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

Begum Turan

01/16/2023, 10:01 PM
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

hfhbd

01/16/2023, 10:03 PM
How do you fetch the "clock" from your webpage?
b

Begum Turan

01/16/2023, 10:05 PM
ask.png
Where am I doing wrong/>
?
h

hfhbd

01/16/2023, 10:08 PM
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

Begum Turan

01/16/2023, 10:15 PM
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

hfhbd

01/16/2023, 10:17 PM
Okay, but this is a simple HTTP get request unable to get updates from the server with Http/1.
b

Begum Turan

01/16/2023, 10:17 PM
because the link you sent it to me was written in fun main
I appreciate your help your explanation
h

hfhbd

01/16/2023, 10:19 PM
No, if you want to get updates via websockets in the browser too, you have to create a js "app" to use websockets.
b

Begum Turan

01/16/2023, 10:20 PM
where should I create "app.js"
the link you sent it to me everything with extension kt not js???
I am so confused
h

hfhbd

01/16/2023, 10:30 PM
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

Begum Turan

01/16/2023, 10:48 PM
where should app.js located?
the file i have to create in the directory
a

Aleksei Tirman [JB]

01/17/2023, 9:11 AM
Here is a simple example of how you can implement it.
b

Begum Turan

01/17/2023, 3:02 PM
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

Aleksei Tirman [JB]

01/17/2023, 3:44 PM
I selected “Kotlin multiplatform”
The Gradle’s wrapper directory is present in the repository.
17 Views