I am experimenting with a Twitter streaming stream, and I am trying to open a stream for a user to consume events as they occur. I use a standard set of classes to create Twitter REST api requests. When using https://userstream.twitter.com/2/user.json in a GET call, the response flow never ends ... I open StreamReader and read the response like I do with any other REST call on Twitter. This is probably obvious to others, but how do I โconsumeโ this stream ... Is there a way to read StreamReader when it reads (before closing)? or maybe there is another method that I can use to use this thread .... again, I apologize if these seams will be elementary for some, but I can not understand this at the moment ... Thanks in advance for any tips or help !!!
Here is the source code from which I started to fix this problem ... This method was fabricated from a set of C # classes that I found on a forum on LinkedIn. On the line that reads "responseData = responseReader.ReadToEnd ()", the method starts to โdrinkโ the stream ... but it does it like a bottomless cup ... reading this data stream in real time before it closes (which essentially as long as I don't stop debugging or kill the process), this is a question that I am solving.
Private Function WebResponseGet(ByVal webRequest As HttpWebRequest) As String Dim responseReader As StreamReader = Nothing Dim responseData As String = "" Try responseReader = New StreamReader(webRequest.GetResponse().GetResponseStream()) responseData = responseReader.ReadToEnd() Catch Throw Finally webRequest.GetResponse().GetResponseStream().Close() responseReader.Close() responseReader = Nothing End Try Return responseData End Function
UPDATE AND RELATED QUESTION:
So, I figured out how to keep the stream open and write it to a file (this will not be the final approach, I am just testing, developing a better way to do this :)
Private Sub DrinkIt(ByVal webRequest As HttpWebRequest) Dim coder As Encoding = Encoding.UTF8 Dim stream_reader As New StreamReader(webRequest.GetResponse().GetResponseStream(), coder, True, 1024) Do While 0 < 1 Dim w As IO.StreamWriter w = File.AppendText(targetFile) Dim c(5) As Char stream_reader.Read(c, 0, c.Length) w.Write(c) w.Close() w.Dispose() Loop stream_reader.Close() stream_reader.DiscardBufferedData() stream_reader.Dispose() End Sub
This writes the open twitter stream to a file and every time I tweet, Retweet, Delete, Direct Message ... etc ... The file grows with JSON objects added to the text. I used Do While 0 <1 for testing here, because I just wanted it to work. I see in the MSDN StreamReader Constructor Description that the new constructor should take a boolean for "leaveOpen", but this argument is not resolved when I try to add this to the constructor ... Does anyone have a good example of how to do this using a forced and infinite loop or just a better approach than that ... I would just like to read the new updates sent each time from Twitter and address them accordingly? . Obviously, there is a way, I am just new to the concept of consuming a stream like this, because it is closed. (** By the way, thanks to the proposal of Dr. Evil, I was a leader in this direction ... This is not quite what he proposed, but here is what brought me here)