HTTP Streaming in Golang
Finally, I’m coming up with an actually useful library and not something just built for the purpose of tests.
Let me introduce gofire.
Establishing the streaming connection
We need to use
net/http/httputil to open a persisted HTTP connection, which requires that we first create
a new TLS connection.
We’re opening a new connection to the server on port 443 (since the connection is encrypted with SSL).
We’re then creating the tls configuration object.
Here, we’re manually specifying the random number used for encryption. But if we didn’t provide it, golang would generate one itself.
The next part of the connection is quite similar to what we’ve considered being straightforward earlier in sending messages.
We create an http request.
And we retrieve a response by actually executing this request with the client previously defined.
Listening for new messages
Now that we are connected to the streaming server, we can start listening to incoming message.
In order to do so, we’re going to use the bufio library.
bufio opens a new I/O connection. Inside which we’re using ReadBytes to wait for any new line and treat it accordingly.
This portion of code will wait until the streaming connection send a new paragraphe (\r), continue the program at that moment.
So we’re waiting until we get an entire line, and display it.
Since we want to be waiting for new lines as long as the program is working though, we’re gonna loop indefinitely inside that part.
We’re basically doing the same as above here, except we’re looping indefinitely when receiving new lines.
The only thing which could break our loop is marking the connection as stale, which will close it and stop the execution.
fun method is a callback used internally to send each new line to the room object.
We could definitely be using a channel there. But since we’re already using one in the public API to send the final message object, I wanted to avoid creating too many threads.
Parsing the message and sending it back
Now that we have received a message, it’s being transmitted to our callback.
Inside which we retrieve the content of the message, in
We’re then generating a new message object and unmarshaling the byte content inside it.
Starting now, we have access to a
Message object with it’s values filled from the content the API gave us.
We can send this object to the public channel.
channel <- message
The public API
Now that we’ve seen how the streaming works under the table, we can more easily understand the public API.
In which we create a new client with our subdomain and API token, along with a room.
channel := room.Listen()
Inside our room, we can start listening for new messages.
After starting listening, the connection to the streaming server is established and we’re ready to receive messages.
So we loop indefinitely, listening for new messages in the channel.
Whenever one is received, go will automatically continue to the next line, which prints the message.
In this example, I’ve taken for granted the golang basics.
If you got lost and just scrolled down here, I recommand that you read the golang book.