Hello, I'm working with a data source that streams json over a websocket.
I'd like to capture this and save it somewhere, maybe eventually in a db
I've been trying to get up to speed on both the websocket and jsonlite packages and to also better understand R connection objects generally, as covered by Jeroen Ooms in this helpful SO post.
A seemingly natural solution would be to use jsonlite::stream_in(), which expects a connection object supplying json.  However, the object returned by websocket::WebSocket() is not that.
My question: how can I best capture the json stream?
And fwiw, my use case streams A LOT of frequent data,
library("websocket")
library("jsonlite")
# ws is an R6 object of class 'WebSocket'
ws <- websocket::WebSocket$new("ws://echo.websocket.org/", autoConnect = FALSE)
ws$onMessage(function(event) {
  cat(event$data)
  ## instead of cat'ing to console, I want to stream the json somewhere usefull
  ## my sense is that this should happen within this event handler
})
ws$connect()
for (i in 1:10) {
  ws$send(jsonlite::toJSON(data.frame(msg = i)))
}
#> Error in wsSend(private$wsObj, msg): invalid state
ws$close()
Created on 2018-10-24 by the reprex package (v0.2.1)
Session info
devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.5.1 (2018-07-02)
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  tz       America/Chicago             
#>  date     2018-10-24
#> Packages -----------------------------------------------------------------
#>  package   * version    date       source                            
#>  backports   1.1.2      2017-12-13 CRAN (R 3.5.0)                    
#>  base      * 3.5.1      2018-07-05 local                             
#>  compiler    3.5.1      2018-07-05 local                             
#>  datasets  * 3.5.1      2018-07-05 local                             
#>  devtools    1.13.6     2018-06-27 CRAN (R 3.5.0)                    
#>  digest      0.6.15     2018-01-28 CRAN (R 3.5.0)                    
#>  evaluate    0.10.1     2017-06-24 CRAN (R 3.5.0)                    
#>  graphics  * 3.5.1      2018-07-05 local                             
#>  grDevices * 3.5.1      2018-07-05 local                             
#>  htmltools   0.3.6      2017-04-28 CRAN (R 3.5.0)                    
#>  jsonlite  * 1.5        2017-06-01 CRAN (R 3.5.0)                    
#>  knitr       1.20       2018-02-20 CRAN (R 3.5.0)                    
#>  later       0.7.5      2018-09-18 cran (@0.7.5)                     
#>  magrittr    1.5        2014-11-22 CRAN (R 3.5.0)                    
#>  memoise     1.1.0      2017-04-21 CRAN (R 3.5.0)                    
#>  methods   * 3.5.1      2018-07-05 local                             
#>  R6          2.2.2      2017-06-17 CRAN (R 3.5.0)                    
#>  Rcpp        0.12.19    2018-10-01 cran (@0.12.19)                   
#>  rlang       0.2.2      2018-08-16 cran (@0.2.2)                     
#>  rmarkdown   1.9        2018-03-01 CRAN (R 3.5.0)                    
#>  rprojroot   1.3-2      2018-01-03 CRAN (R 3.5.0)                    
#>  stats     * 3.5.1      2018-07-05 local                             
#>  stringi     1.2.2      2018-05-02 CRAN (R 3.5.0)                    
#>  stringr     1.3.1      2018-05-10 CRAN (R 3.5.0)                    
#>  tools       3.5.1      2018-07-05 local                             
#>  utils     * 3.5.1      2018-07-05 local                             
#>  websocket * 0.0.0.9001 2018-09-21 Github (rstudio/websocket@ef78ced)
#>  withr       2.1.2      2018-03-15 CRAN (R 3.5.0)                    
#>  yaml        2.1.19     2018-05-01 CRAN (R 3.5.0)
Possible solution patterns I've considered:
- turn the ws object above into a connection object, which is expected by json_lite::stream_in()
- manually parse json and append to some object/ insert into a db (I've done the former, but it seems inefficient and non-scalable)
Any guidance would be appreciated.
 include this.:
 include this.: