Search code examples
time-seriesmeanmodelinglarge-data

Printing datastream from socket


I am trying to connect to a socket which provides a feed of stock prices (stockID,price), and then print it. The stream is endless. My problem is that I cannot print it.

To begin with, I create a connection:

con <- socketConnection(host = "88.99.38.191", port = 1337, open = "r")

then i set a variable reading all the lines. data <- readLines(con,-1)

Then print(data)

the problem is that depending on the time gap between executing the connection and setting variable data, the latter receives a different number of values, and that's it.

I am trying to print somehow the entire stream. If I use while (TRUE) { print(data) }

it just prints endlessly the data in loops.

Any idea how to implement that?

My ultimate goal is to calculate the moving average for each ID.


Solution

  • For those interested this is the answer.

    con <- socketConnection(host = "88.99.38.191", port = 1337, open ="r",blocking = T,server=FALSE)
    
    while(TRUE) {   
    data <- readLines(con,1)
    print(data)
    }
    

    The problem with my initial approach was that I didn't use the blocking = T property for socketConnection. Further info can be found here.