# NOT RUN {
# Start an R process (R#1) and make it a server
library(svSocket)
serverport <- 8888 # Port 8888 by default, but you can change it
startSocketServer(port = serverport)
# 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
getSocketClients() # You should have one client registered
# Send something to all clients from R#1
sendSocketClients("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
stopSocketServer(port = serverport)
# }
Run the code above in your browser using DataLab