In a previous post, we looked at how to consume a live streaming API from Twitter. That was great and all, but you’re not going to become the next Twitter by consuming content — you need to have your own live streaming API. In this post we’ll look at how to create a basic streaming API that others can open a streaming connection to and get real-time updates.
So, what groundbreaking web service will we be exposing to the masses? We’ll be streaming the web server’s current datetime! Ingenious, I know! Hopefully, you’ll be able to adapt this to a slightly more engaging experience.
To start, let’s just take an ASP.NET MVC project and code the default controller action. We clear all the headers of the response and add the “application/json” header. Since we won’t be returning a view in the traditional sense of ASP.NET MVC, we don’t have to create an actual view. We’ll just pause the thread for 1 second and then return the current timestamp for as long as the service is running.
On the consumer side, we’ll just refactor the code we had in the previous post to connect to the URL of the project we just created. I’m running in on the local development machine so your port will likely be different.
You’ll notice we added some exception handling logic in case we lose our connection to our streaming API. In this case, we’ll just keep trying to re-connect to the stream. I’ll show you a sample of this in the last part of the post.
Once we have all the bits in place, we run the ASP.NET MVC application first so it is ready to start handling requests. Then, we create multiple instances of our consumer console application to simulate multiple clients connecting to the streaming API. You’ll end up seeing something like this:
To simulate what would happen on the consumer side if there was a broken connection or other exception on the server side, I killed the local development server for a few seconds and then restarted it. Below you can see that we recovered after a few exception handling loops.
Of course, this post was just intended to provide a possible option for implementing a streaming API with ASP.NET MVC. For a more realistic scenario you would likely have a queue listener around our Thread.Sleep() code. As messages are picked up from the queue, you could stream it out and immediately start listening for the next message on the queue.
In a follow-up post, I will demo this same concept using Node.js.
Hope this helps.