This is an R WebSocket client library backed by the websocketpp C++ library. WebSocket I/O is handled on a separate thread from R.
library(websocket)
<- WebSocket$new("ws://echo.websocket.org/", autoConnect = FALSE)
ws $onOpen(function(event) {
wscat("Connection opened\n")
})$onMessage(function(event) {
wscat("Client got msg: ", event$data, "\n")
})$onClose(function(event) {
wscat("Client disconnected with code ", event$code,
" and reason ", event$reason, "\n", sep = "")
})$onError(function(event) {
wscat("Client failed to connect: ", event$message, "\n")
})$connect() ws
(If you’re not writing these commands at a console—for instance, if
you’re using a WebSocket as part of a Shiny app—you can leave off
autoConnect=FALSE
and ws$connect()
. These are
only necessary when the creation of the WebSocket object and the
registering of the
onOpen
/onMessage
/onClose
handlers
don’t happen within the same function call, because in those cases the
connection may open and/or messages received before the handlers are
registered.)
Once connected, you can send commands as follows:
# Send text messages
$send("hello")
ws
# Send binary messages
$send(charToRaw("hello"))
ws
# Finish
$close() ws
Note that if you want to send()
a message as soon as it
connects, without having to wait at the console, you can put the
ws$send()
inside of the ws$onOpen
callback, as
in:
<- WebSocket$new("wss://echo.websocket.org/")
ws $onMessage(function(event) {
wscat("Client got msg: ", event$data, "\n")
})$onOpen(function(event) {
ws$send("hello")
ws })
The websocket (client) package can be used with a WebSocket server, implemented as a httpuv web application, to act as a WebSocket proxy. This proxy can be modified do things like log the traffic in each direction, or modify messages sent in either direction.
The proxy will listen to port 5002 on the local machine, and connect to the remote machine at wss://echo.websocket.org:80. In this example, after starting the proxy, we’ll connect to it with a WebSocket client in a separate R process, then send a message from that process. Here’s what will happen in the proxy:
"hello"
."hello"
from the client, convert
it to "HELLO"
, then send it to the remote echo server."HELLO"
and send it
back."HELLO"
from the server, convert
it to "HELLO, world"
, then send it to the client."HELLO, world"
.To run this proxy, run the code below in an R session:
library(curl)
library(httpuv)
library(websocket)
# URL of the remote websocket server
<- "echo.websocket.org:80"
target_host # Should be "ws" or "wss"
<- "ws"
target_protocol
# Port this computer will listen on
<- 5002
listen_port
# ==============================================================================
# Functions for translating header strings between HTTP request and Rook formats
# ==============================================================================
<- function(req, host) {
req_rook_to_curl # Rename headers. Example: HTTP_CACHE_CONTROL => Cache-Control
<- as.list(req)
r
# Uncomment to print out request headers
# cat("== Original ==\n")
# cat(capture.output(print(str(r))), sep = "\n")
<- r[grepl("^HTTP_", names(r))]
r <- names(r)
nms <- sub("^HTTP_", "", nms)
nms <- tolower(nms)
nms <- gsub("_", "-", nms, fixed = TRUE)
nms <- gsub("\\b([a-z])", "\\U\\1", nms, perl = TRUE)
nms names(r) <- nms
# Overwrite host field
$Host <- host
r
# Uncomment to print out modified request headers
# cat("== Modified ==\n")
# cat(capture.output(print(str(r))), sep = "\n")
r
}
<- function(resp) {
resp_httr_to_rook <- as.integer(sub("^HTTP\\S+ (\\d+).*", "\\1", curl::parse_headers(resp$headers)[1]))
status list(
status = status,
headers = parse_headers_list(resp$headers),
body = resp$content
)
}
# ==============================================================================
# Websocket proxy app
# ==============================================================================
# These functions are called from the server app; defined here so that they
# can be modified while the application is running.
<- function(req) {
onHeaders # Print out the headers received from server
# str(as.list(req$HEADERS))
NULL
}
<- function(req) {
call <- req_rook_to_curl(req, target_host)
req_curl <- new_handle()
h do.call(handle_setheaders, c(h, req_curl))
<- curl_fetch_memory(paste0("http://", target_host, req$PATH_INFO), handle = h)
resp_curl resp_httr_to_rook(resp_curl)
}
<- function(clientWS) {
onWSOpen # The httpuv package contains a WebSocket server class and the websocket
# package contains a WebSocket client class. It may be a bit confusing, but
# both of these classes are named "WebSocket", and they have slightly
# different interfaces.
<- websocket::WebSocket$new(paste0(target_protocol, "://", target_host))
serverWS
<- list()
msg_from_client_buffer # Flush the queued messages from the client
<- function() {
flush_msg_from_client_buffer for (msg in msg_from_client_buffer) {
$send(msg)
serverWS
}<<- list()
msg_from_client_buffer
}$onMessage(function (isBinary, msgFromClient) {
clientWScat("Got message from client: ", msgFromClient, "\n")
# NOTE: This is where we modify the messages going from the client to the
# server. This simply converts to upper case. You can modify to suit your
# needs.
<- toupper(msgFromClient)
msgFromClient cat("Converting toupper() and then sending to server: ", msgFromClient, "\n")
if (serverWS$readyState() == 0) {
length(msg_from_client_buffer) + 1] <<- msgFromClient
msg_from_client_buffer[else {
} $send(msgFromClient)
serverWS
}
})$onOpen(function(event) {
serverWS$onMessage(function(msgFromServer) {
serverWScat("Got message from server: ", msgFromServer$data, "\n")
# NOTE: This is where we could modify the messages going from the server
# to the client. You can modify to suit your needs.
<- paste0(msgFromServer$data, ", world")
msg cat('Appending ", world" and then sending to client: ', msg, "\n")
$send(msg)
clientWS
})flush_msg_from_client_buffer()
})
}
# Start the websocket proxy app
<- startServer("0.0.0.0", listen_port,
s list(
onHeaders = function(req) {
onHeaders(req)
},call = function(req) {
call(req)
},onWSOpen = function(clientWS) {
onWSOpen(clientWS)
}
)
)
# Run this to stop the server:
# s$stop()
# If you want to run this code with `Rscript -e "source('server.R')"`, also
# uncomment the following so that it doesn't immediately exit.
# httpuv::service(Inf)
In a separate R session, you can connect to this proxy and send a message.
# Connect to proxy
<- websocket::WebSocket$new("ws://localhost:5002")
ws $onMessage(function(event) {
wscat(paste0('Received message from proxy: "', event$data, '"\n'))
})# Send message to proxy
$send("hello")
ws#> Received message from proxy: "HELLO, world"
In the R process running the proxy, you will see:
Got message from client: hello
Converting toupper() and then sending to server: HELLO
Got message from server: HELLO
Appending ", world" and then sending to client: HELLO, world
This is a simple WebSocket echo server implemented with httpuv. You can run it and interact with using the WebSocket client code below.
library(httpuv)
cat("Starting server on port 8080...\n")
<- startServer("0.0.0.0", 8080,
s list(
onHeaders = function(req) {
# Print connection headers
cat(capture.output(str(as.list(req))), sep = "\n")
},onWSOpen = function(ws) {
cat("Connection opened.\n")
$onMessage(function(binary, message) {
wscat("Server received message:", message, "\n")
$send(message)
ws
})$onClose(function() {
wscat("Connection closed.\n")
})
}
) )
This code will connect to the echo server and send a message:
library(websocket)
<- WebSocket$new("ws://127.0.0.1:8080/",
ws headers = list(Cookie = "Xyz"),
accessLogChannels = "all" # enable all websocketpp logging
)
$onOpen(function(event) {
wscat("Connection opened\n")
})$onMessage(function(event) {
wscat("Client got msg: ", event$data, "\n")
})$onClose(function(event) {
wscat("Client disconnected with code ", event$code,
" and reason ", event$reason, "\n", sep = "")
})$onError(function(event) {
wscat("Client failed to connect: ", event$message, "\n")
})
$send("hello")
ws
$send(charToRaw("hello"))
ws
$close() ws
If you want it to send the message as soon as it connects (without
having to wait for a moment at the console), you can tell it to do that
in the onOpen
callback:
$onOpen(function(event) {
wscat("Connection opened\n")
$send("hello")
ws })