-
Notifications
You must be signed in to change notification settings - Fork 257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Websocket support in Plumber #723
Comments
What would be the use case? plumber does not support websockets at the moment. onWSOpen = function(ws){
warning("WebSockets not supported.")
}, |
My server is performing long running computation (could be even hours), and I would like it I have understood that Websockets are the "correct" solution to this, and I have Jarkko
|
@jttoivon What plumber feature are you looking to leverage in this case? Seem like httpuv would fit your need, no? |
Yes, maybe I should move from Plumber to httpuv. Currently I'm only using the http request parameter parsing, and #' @assets ../static /static And for serialisation of function return values. Thanks! |
Going to reopen this to have more discussion... Since For followup... Due to this line... (specifically passing in Line 214 in 24614d5
ws methods to the Plumber object directly.
This will not work due to
#> Error in pr$onWSOpen <- function(ws) { :
#> cannot change value of locked binding for 'onWSOpen' Even if To get around this, maybe we could update the Plumber definition. Plumber$onWSOpen <- function(ws) {
if (private$ws_open) {
private$ws_open(ws)
}
invisible(self)
}
# Same for onWSMessage and onWSClose We could add them via Plumber$websocket <- function(open, message, close) {
private$ws_open <- open
private$ws_message <- message
private$ws_close <- close
} ( In the end..
This even opens the door for |
I could not wrap my head around replicating plumber feature around websocket. If you have an idea, I could build a prototype. I know grpc, http/2, tcp. I thought web sockets was more like a binary messaging system. Client and server had to know how to work with messages. |
Maybe we just start with the If we could update this method: Lines 757 to 759 in 24614d5
To be : onWSOpen = function(ws) {
if (private$ws_open) {
private$ws_open(ws)
}
invisible(self)
}, and add the public method of websocket = function(open = NULL) {
if (!is.null(open)) stopifnot(is.function(open))
private$ws_open <- open
} and the private variable We should be able to test it by adding this to the plumber definition #' @get /
function() { "running" }
#' @plumber
function(pr) {
pr$websocket(
function(ws) {
print("It opened!") }
# echo
ws$onMessage(function(binary, message) {
ws$send(message)
})
}
)
}
Testing it with the example from the httpuv readme...
(We'll need to test using a different R process as ws <- websocket::WebSocket$new("ws://127.0.0.1:8080/")
ws$onMessage(function(event) {
cat("Client received message:", event$data, "\n")
})
# Wait for a moment before running next line
ws$send("hello world")
# Close client
ws$close() (note: all codes are untested) |
Suggestions for use cases:
|
I'm pitching that out there. What about a type of endpoint for long running process that reports on progress when the endpoint is already executing? Deal with session? Is plumber trying to be grpc? Interesting to see how this will all evolve. |
It could also be beneficial for creating a chatbot session and much more... |
Hi,
Does Plumber have any support for websockets? There seems to be support in httpuv, which
I suppose Plumber is based on. Or do I have to open another port with httpuv to listen for websocket
connections?
Jarkko
The text was updated successfully, but these errors were encountered: