Hi guys, according to <https://github.com/ktorio/k...
# ktor
s
Hi guys, according to https://github.com/ktorio/ktor/blob/main/ktor-server/ktor-server-plugins/ktor-server-sse/common/src/io/ktor/server/sse/SSEServerContent.kt It says that : Please note that you generally shouldn't use this object directly but use [SSE] plugin with routing builders [sse] instead. Can I ask in what cases should we use this object?
k
custom implementations? Probably this object could change but the routing has stricter api and wont change that often?
a
To implement a custom logic of responding instead of the default one.
s
@Aleksei Tirman [JB] thank you for responding. my use case is whenever client does a post request to server the server can open a sse stream but i’m not sure how to achieve this
a
You can use the tail card route path to achieve that:
Copy code
install(SSE)

routing {
    route("{...}", <http://HttpMethod.Post|HttpMethod.Post>) {
        sse {
            while (true) {
                delay(1000)
                send(ServerSentEvent("hello"))
            }
        }
    }
}
s
thank you @Aleksei Tirman [JB] i’ll try it out and ask if i have more qns
a
You can find the information about different kinds of route paths in the documentation.
👍 1
s
@Aleksei Tirman [JB] the example you showed opens one sse session right? How can we open sessions per request
a
In the example, an SSE session is established for every POST request.
s
oh thank you
Hi @Aleksei Tirman [JB], suppose I have a client and a server and whenever i make a post request to the server and i open a sse session 1. can i do it like this ? install(SSE) routing { post("/sse") { sse { } } } } 2. How can the client connect to this session?
a
1. No, you can't do it like this. You can use my example above. 2. You can use the SSE client plugin to do that.
s
@Aleksei Tirman [JB] 1. Thank you 2. suppose the route is /sse so everytime a request is made there a sse session is established, the client should connect to /sse ?
a
Yes, the client should send the POST request to the
/sse
endpoint
s
but for listening to sse events where should it connect to or listen to? i suppose it should be the same post endpoint right so /sse
just to clarify client sends request to server via post endpoint and server opens a sse session for each post request
a
but for listening to sse events where should it connect to or listen to?
What do you mean?
just to clarify client sends request to server via post endpoint and server opens a sse session for each post request
Yes
s
client sends request to server via post endpoint and server opens a sse session for each post request and client should connect to the same post endpoint using SSE client plugin?