# Simple echo server
h <- handler_ws("/ws", function(ws, data) ws$send(data))
# With connection tracking
clients <- list()
h <- handler_ws(
"/chat",
on_message = function(ws, data) {
# Broadcast to all
for (client in clients) client$send(data)
},
on_open = function(ws, req) {
clients[[as.character(ws$id)]] <<- ws
},
on_close = function(ws) {
clients[[as.character(ws$id)]] <<- NULL
},
textframes = TRUE
)
Run the code above in your browser using DataLab