rstudio/websocket

Run webscoket 'forever'

MislavSag opened this issue · 1 comments

I want to use WebSocket explained on this site:
https://site.financialmodelingprep.com/developer/docs/websocket-api/#Python

I don't understand which method can replace last line of the code:

print(ws.recv())

I assume I have to make constant (streaming) requests on server, but I don't know how to do it.

Here is my try:

library(websocket)


ws <- WebSocket$new("wss://websockets.financialmodelingprep.com", autoConnect = FALSE)
ws$onOpen(function(event) {
  cat("Connection opened\n")
})
ws$onMessage(function(event) {
  cat("Client got msg: ", event$data, "\n")
  cat("Client got msg: ", event, "\n")
})
ws$onClose(function(event) {
  cat("Client disconnected with code ", event$code,
      " and reason ", event$reason, "\n", sep = "")
})
ws$onError(function(event) {
  cat("Client failed to connect: ", event$message, "\n")
})
ws$connect()


login = list(
  event = 'login',
  data =  list(
    apiKey = "xxx"
  )
)

subscribe = list(
  event = 'subscribe',
  data =  list(
    ticker = "aapl"
  )
)

unsubscribe = list(
  event = 'unsubscribe',
  data =  list(
    ticker = "aapl"
  )
)

# Send text messages
ws$send(jsonlite::toJSON(login))

ws$send(jsonlite::toJSON(subscribe))

ws$send(jsonlite::toJSON(unsubscribe))

# Finish
ws$close()

I don't get any return message when calling the method:

ws$send(jsonlite::toJSON(subscribe))

Also, I am not sure how to implement this part of the sample (python code):

ws = websocket.WebSocket(sslopt={"cert_reqs": ssl.CERT_NONE})

and if executing this code affects results.

mgei commented

Could you find a solution?