# NOT RUN {
# Start an R process (R#1) and make it a server
library(svSocket)
server_port <- 8888 # Port 8888 by default, but you can change it
start_socket_server(port = server_port)
# Start a second R process (R#2) and run this code in it (the R client):
library(svSocket)
# Connect with the R socket server
con <- socketConnection(host = "localhost", port = 8888, blocking = FALSE)
# Now, go back to the server R#1
get_socket_clients() # You should have one client registered
# Send something to all clients from R#1
send_socket_clients("Hi there!")
# Switch back to client R#2
# Since the connection is not blocking, you have to read lines actively
readLines(con)
# Note the final empty string indicating there is no more data
close(con) # Once done...
# Switch to the R#1 server and close the server
stop_socket_server(port = server_port)
# }
Run the code above in your browser using DataLab