![]() And if neither works, then a hosted deployment of Redis or your favorite database can work too.Īn option that is specific to the Twilio serverless platform is to store the counter in an environment variable. On the Twilio serverless platform, Twilio Sync is the service of choice. For AWS, DynamoDB is a convenient option that is often paired with Lambda. The solution for these restricted platforms is to use a “database as a service”. What can you do when working with these platforms? This is often the case with serverless deployment platforms such as AWS Lambda or Twilio Functions. You may be in a situation where a global variable can’t work, and you also cannot add Redis or another database. What kind of database is best for maintaining a counter? My personal preference is to use Redis, which provides atomic counting primitives such as INCR and DECR. A counter that is stored in a database makes it possible to scale the server horizontally, so this is the preferred method for larger server deployments. If a global variable isn’t an appropriate solution for your server configuration, then a database-stored counter is often used. When this is necessary, a Mutex or Lock should be sufficient to increment and decrement the counter atomically. The big advantage of this method is that it is very easy to implement, but on the other hand, it limits your scalability, as this solution only works when you have a single server process.ĭepending on the programming language and the concurrency model used by your application, you may also have to do some additional work to prevent race conditions if two or more request handlers try to modify the counter global variable at the same time. The most straightforward way to implement a counter is to use a global variable in the process space of your server. This may seem like a trivial task, but how to store a counter in a web application does deserve some thought, in particular regarding choosing an implementation that is compatible with the concurrency model used by your back end. Updating the count every five seconds is reasonable for the streamer, but for the audience I would suggest a longer interval of at least one or two minutes. Something important to keep in mind is that you do not want your server flooded with requests to get viewer counts from clients watching the livestream. Once the livestream starts, the (currently faked) viewer count will appear in the top-left corner, together with the “LIVE” indicator:Īdding the viewer count to the audience page would work in the same way, so I’ll leave it as an exercise for you. Then in your browser, visit and start a new livestream. Starting from Mia Adjei’s livestreaming project, a new route that returns the current viewer count can be added to the server. The easiest part of this project is to display the viewer counts, as this is just an exercise in front and back end development. You can review the features and limitations of a free Twilio account. If you are new to Twilio click here to create a free account now and receive $10 credit when you upgrade to a paid account. To state the obvious, you will also need a Twilio account. For the examples shown in this article, I’m going to modify Mia Adjei’s project. If you followed one of the tutorials I recommended above, you already have a complete project that you can use to experiment with adding viewership counts. To apply what you learn in this article, you will need your own livestreaming application. My colleague Mia Adjei wrote a Twilio Live tutorial using Express.js, and I have also written one using the Twilio serverless platform, both of which show you how to build a complete livestreaming project step by step. If you haven’t worked with Twilio Live before, I recommend that you follow an introductory tutorial first. This article does not have any requirements beyond having some familiarity with the Twilio Live product. But how do you do that? There are many different ways to do it, and the best method to use will depend on your needs and your technology stack.Īre you ready to learn how to count your livestream viewers? In this article I’ll show you a few possible implementations with varying levels of complexity and flexibility. An important aspect of running a Twilio Live streaming experience is to track your viewership.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |