Skip to content
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

ws experiment #724

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

ws experiment #724

wants to merge 4 commits into from

Conversation

meztez
Copy link
Collaborator

@meztez meztez commented Dec 15, 2020

Related: #723

You get an Error in wsSend(private$wsObj, msg) : invalid state when you try to use websockets while a request is being processed by the regular REST API over http.

I've yet to find a way to leverage websockets over regular plumbing, this need more thinking.

PR task list:

  • Update NEWS
  • Add tests
  • Update documentation with devtools::document()

@meztez
Copy link
Collaborator Author

meztez commented Dec 15, 2020

I think this is more interesting.

api

#* @post /sleeper
#* @get /sleeper
#* @param payload:df
#* @parser json
#* @serializer unboxedJSON
function(payload = matrix(1:8, 2, 4), req) {
  a <- rowSums(payload)
  req$ws$send("Almost done")
  b <- colSums(payload)
  req$ws$send("Calculation completed")
  list(a, b)
}

Using websockets

ws <- websocket::WebSocket$new("ws://127.0.0.1:3946/sleeper")
ws$onMessage(function(res) { cat(res$data, "\n") })
ws$send('{"payload":[[1,3,5,7],[2,4,6,8]]}')
ws$close()

results

> ws <- websocket::WebSocket$new("ws://127.0.0.1:9629/sleeper")
> ws$onMessage(function(res) { cat(res$data, "\n") })
> ws$send('{"payload":[[1,8,5,7],[2,5,6,50]]}')
Almost done 
Calculation completed 
_status_ 200 
_headers_ Content-Type=application/json 
_body_ nextmessage 
[[21,63],[3,13,11,57]] 
> ws$close()

Then you can get intermediate response before getting the final response. Still use case to cover, I think this is a good start.

pr$serve(req, res)
ws$send(paste("_status_", res$status))
ws$send(paste("_headers_", paste(names(res$headers), unlist(res$headers), sep = "=", collapse = ";")))
ws$send("_body_ nextmessage")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It probably makes more sense to only return res$body in websockets context?

@schloerke schloerke added this to the should address milestone Dec 30, 2020
@meztez
Copy link
Collaborator Author

meztez commented Jan 1, 2021

Would websockets bypass cookie or apikey filter?

Most likely they should not be enabled by default.

@CLAassistant
Copy link

CLAassistant commented Nov 8, 2023

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants