<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Paolo Rovelli on Medium]]></title>
        <description><![CDATA[Stories by Paolo Rovelli on Medium]]></description>
        <link>https://medium.com/@rovellipaolo?source=rss-5213b4b41f52------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*-Q4VU93aB4-7dAijj6psrA.jpeg</url>
            <title>Stories by Paolo Rovelli on Medium</title>
            <link>https://medium.com/@rovellipaolo?source=rss-5213b4b41f52------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 20:19:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rovellipaolo/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Asynchronous development in Android: RxJava Vs. Kotlin Flow]]></title>
            <link>https://medium.com/makingtuenti/asynchronous-development-in-android-rxjava-vs-kotlin-flow-f7fdf2e2f81b?source=rss-5213b4b41f52------2</link>
            <guid isPermaLink="false">https://medium.com/p/f7fdf2e2f81b</guid>
            <category><![CDATA[reactive-programming]]></category>
            <category><![CDATA[rxjava]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin-coroutines]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Paolo Rovelli]]></dc:creator>
            <pubDate>Fri, 27 Dec 2019 09:19:45 GMT</pubDate>
            <atom:updated>2021-02-28T23:54:28.455Z</atom:updated>
            <content:encoded><![CDATA[<h3>Asynchronous development in Android: RxJava Vs. Kotlin Flow</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d6CooJUcJXk6ssMWhQp1mg.jpeg" /></figure><p>Lately, a lot of exciting things are going on in the Android world.<br>Surely, the most eye-catching one is <a href="https://developer.android.com/jetpack/compose">Jetpack Compose</a>. However, personally, I feel that <a href="https://kotlinlang.org/docs/reference/coroutines/flow.html">Kotlin Flow</a> is as interesting as the previous one and could potentially become a killer-feature for asynchronous development (not only in Android, but in the Kotlin ecosystem as a whole).</p><p>Kotlin already had an existing way to handle streams of data in coroutines, which is the <a href="https://kotlinlang.org/docs/reference/coroutines/channels.html">Channels</a> API. However, Channels represent a hot stream of data and therefore are only fit for data sources that should always exist, even when the app is not requesting for them (e.g. incoming network connections, event streams, …). On the other hand, the recently released Flow API represents a cold stream of data and therefore is fit where Channels are not.</p><blockquote><em>“Talk is cheap. Show me the code.” (Linus Torvalds)</em></blockquote><p>Let’s imagine that we want to write a contacts app.</p><blockquote><strong>(!) DISCLAIMER (!)<br></strong>There are literally thousands of ways to write the same thing, and I would not define myself as an expert in neither RxJava nor Kotlin Flow.<br>Therefore, even if I will try to be as impartial as possible, there might be some innocent mistakes. Sorry in advance if that is the case. :)</blockquote><h3>Dependencies</h3><p>First of all, let’s import some dependencies to play with:</p><pre><strong>// Android</strong><br>implementation &quot;androidx.core:core-ktx:1.1.0&quot;<br>implementation &quot;androidx.lifecycle:lifecycle-extensions:2.1.0&quot;<br>implementation &quot;androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0&quot;<br>testImplementation &quot;androidx.arch.core:core-testing:2.1.0&quot;</pre><pre><strong>// RxJava</strong><br>implementation &quot;io.reactivex.rxjava2:rxandroid:2.1.1&quot;<br>implementation &quot;io.reactivex.rxjava2:rxkotlin:2.4.0&quot;</pre><pre><strong>// Coroutines</strong><br>implementation &quot;org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2&quot;<br>implementation &quot;org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2&quot;<br>testImplementation &quot;org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.2&quot;</pre><pre><strong>// JUnit + Mockito</strong><br>testImplementation &quot;junit:junit:$junit_version&quot;<br>testImplementation &quot;com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0&quot;</pre><h3>Repository</h3><p>Now, let’s create the first piece of our app: a repository.<br>This is needed to encapsulate how we will retrieve and persist contacts in our app. For the sake of simplicity, we will just query a fake data source and go through each contact to emit it in an observable. We will neither merge contacts from different data sources (e.g. local and cloud contacts) nor map them from the data model to a domain model.</p><pre>interface DataSource {<br>    fun getAll(): List&lt;Contact&gt;<br>}</pre><h4>RxJava</h4><p>Now, using <a href="https://github.com/ReactiveX/RxJava">RxJava</a>, we would probably do something like the following.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a8f21e8ed3503a1ba061b47c8a4de1b/href">https://medium.com/media/7a8f21e8ed3503a1ba061b47c8a4de1b/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/11a1c273448f5cf5e68a9da207a8e1ce/href">https://medium.com/media/11a1c273448f5cf5e68a9da207a8e1ce/href</a></iframe><p>RxJava offers a really powerful and complete API, including several built-in builders to <a href="https://github.com/ReactiveX/RxJava/wiki/Creating-Observables">create</a> and <a href="https://github.com/ReactiveX/RxJava/wiki/Combining-Observables">combine</a> different types of observables. Its API is so exhaustive that, at first, might feel a bit overwhelming. <br>But, as it is often the case, power comes with a price.</p><blockquote><strong>Neither the </strong>RxJava<strong> implementation nor the tests are totally straightforward, at least if you don’t have some rudimentary knowledge of reactive streams.</strong></blockquote><p>In particular, in the implementation, we need to explicitly wrap the data source call in a try-catch to be able to emit exactly once either the <a href="http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Observer.html#onComplete--">onComplete()</a>, to signal the stream completion, or the <a href="http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Observer.html#onError-java.lang.Throwable-">onError()</a>.<br>In the tests, we need to call the <a href="http://reactivex.io/RxJava/javadoc/io/reactivex/Observable.html#test--">test()</a> to create a <a href="http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/observers/TestObserver.html">TestObserver</a> and subscribe it to the observable, as well as the <a href="http://reactivex.io/RxJava/javadoc/io/reactivex/observers/DisposableObserver.html#dispose--">dispose()</a> to cancel the TestObserver.</p><h4>Kotlin Flow</h4><p>On the other hand, using Kotlin Flow instead of RxJava, we would probably do something like the following.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/151dc4bc1097f11db25034298d378940/href">https://medium.com/media/151dc4bc1097f11db25034298d378940/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98fb0596906ea63cc90d703b58872896/href">https://medium.com/media/98fb0596906ea63cc90d703b58872896/href</a></iframe><p>Kotlin Flow offers a simpler but extensible API, including several already available <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/index.html">extension functions</a>. Its API is so “Kotlinic” that feels well-integrated in the language.</p><p>As you can see, in the implementation, the <a href="http://reactivex.io/documentation/operators/create.html">Observable.create { emitter -&gt; ... }</a> builder is replaced by the <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/flow.html">flow { ... }</a> one and we can get rid of the explicit try-catch. Flow will automatically close the stream of data for us and, if the data source throws an exception, this will be passed to the upper layers.<br>Testing also seems more straightforward, or at least more coherent with the Kotlin standard library. Indeed, in the first test case we can simply convert the Flow in a List with the <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/to-list.html">toList()</a> extension function, while in the second one we can actually leverage the <a href="https://junit.org/junit4/javadoc/latest/org/junit/Test.html#expected()">@Test(expected=...)</a> JUnit annotation.</p><blockquote>That said, even with Kotlin Flow there is a bit of <em>“magic”. Coroutines </em>“magic”.</blockquote><p>In particular, we need to run the tests using the experimental <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/run-blocking-test.html">runBlockingTest</a> builder, and therefore we need to mark them all as @ExperimentalCoroutinesApi. This is is needed because all Flow operators are suspend functions and thus must be called either from a coroutine or another suspend function.<br>Note that, to run a coroutine while blocking the current thread until it’s completed, we could have actually used the standard <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/run-blocking.html">runBlocking</a> builder. However, the advantage of using runBlockingTest over runBlocking is that the former will immediately progress past delays.</p><h3>UseCase</h3><p>Then, let’s create the second piece of our app: a use case.<br>This is needed to encapsulate the business logic that we will apply over the retrieved contacts. For the sake of simplicity, we will just call our repository straightforward. We will neither perform any business logic nor combine data from multiple repositories.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8dc31d618b7ee792325961926f79a435/href">https://medium.com/media/8dc31d618b7ee792325961926f79a435/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2de471ad46ae65f987393f93fbe6a3f9/href">https://medium.com/media/2de471ad46ae65f987393f93fbe6a3f9/href</a></iframe><p>As you can see, the two implementations are nearly identical.</p><h3>ViewModel</h3><p>Finally, let’s create the last piece of our app: a ViewModel.<br>This is needed to encapsulate the presentation logic that we will apply over the retrieved contacts. For the sake of simplicity, we will just launch our use case and update the list of contacts.</p><h4>RxJava</h4><p>Let&#39;s start also this time with the RxJava version.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ccf6e6abbd1507e5f1a68e8ca4ae7e0e/href">https://medium.com/media/ccf6e6abbd1507e5f1a68e8ca4ae7e0e/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aeab90fb6e5a1ecac59f871e205effbc/href">https://medium.com/media/aeab90fb6e5a1ecac59f871e205effbc/href</a></iframe><blockquote>Again, we can see all the power and potential of RxJava together with its intrinsic complexity.</blockquote><p>In particular, in the implementation, we explicitly subscribe to the stream on the <a href="http://reactivex.io/RxJava/javadoc/io/reactivex/schedulers/Schedulers.html#io--">Schedulers.io()</a> and observe on the <a href="https://javadoc.io/doc/io.reactivex/rxandroid/latest/rx/android/schedulers/AndroidSchedulers.html#mainThread()">AndroidSchedulers.mainThread()</a>, which is needed to execute actions on the Android UI thread.<br>In the tests, we need to use the <a href="https://javadoc.io/doc/io.reactivex.rxjava2/rxandroid/latest/io/reactivex/android/plugins/RxAndroidPlugins.html">RxAndroidPlugins</a> to set the main thread scheduler handler, which in this case is set to <a href="http://reactivex.io/RxJava/javadoc/io/reactivex/schedulers/Schedulers.html#trampoline--">Schedulers.trampoline()</a>.</p><p>That said, besides a bit of reactive <em>“magic”</em>, both the RxJava implementation and its tests are quite easy to follow.</p><h4>Kotlin Flow</h4><p>In this case, if we replace RxJava with Kotlin Flow, we would just need to change the loadContacts() method of our ViewModel.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6aa4ea86ed707893a450b2064be40a27/href">https://medium.com/media/6aa4ea86ed707893a450b2064be40a27/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a702a658c2a522ca18d0aadfa4041c9/href">https://medium.com/media/1a702a658c2a522ca18d0aadfa4041c9/href</a></iframe><p>Kotlin Flow has transparent back-pressure management, thanks to the use of Kotlin suspending functions. Indeed, as said, all Flow operators are suspend functions.</p><blockquote>But, alas, also Kotlin Flow has its own complexity.</blockquote><p>In particular, in the implementation, we launch the use case inside the <a href="https://developer.android.com/topic/libraries/architecture/coroutines#viewmodelscope">viewModelScope</a> so that the coroutines context is inherited from the ViewModel and it will be canceled when ViewModel is cleared.</p><p><strong>NOTE:</strong> using viewModelScope requires the<a href="https://developer.android.com/kotlin/ktx#viewmodel"> Lifecycle ViewModel KTX</a> library with version <em>2.1.0</em> or higher.</p><p>Furthermore, it&#39;s important to note that by default the streams in Flow are executed sequentially, in a single coroutine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/685/1*RRQo-F2wrGiL1eyZ8N65Pg.jpeg" /><figcaption>Data flow without buffering</figcaption></figure><p>To execute them in parallel, running the collector in a separate coroutine, we need to use the experimental <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/buffer.html">buffer()</a> operator. This way, as soon as the repository has computed the contact and given it to the ViewModel, it can immediately start producing the next one and the ViewModel works on previous contact in parallel.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/525/1*VHwNDufMm7frJOIWKQLgAg.jpeg" /><figcaption>Data flow with buffering</figcaption></figure><p>However, since the buffer() operator is still marked as experimental, we need to mark the entire loadContacts() method as @ExperimentalCoroutinesApi.</p><p>Finally, in the tests, we need to set up and tear down the test dispatcher for the main thread.</p><p>That said, besides a bit of coroutines <em>“magic”</em>, also the Flow implementation and its tests are quite easy to follow.</p><h4>Kotlin Flow (alternative syntax)</h4><p>If you prefer, in Flow, there is also an alternative fluent-style syntax which uses the experimental onEach(), catch() and launchIn() operators.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38c35cd414ec5343f56c13c067d7259d/href">https://medium.com/media/38c35cd414ec5343f56c13c067d7259d/href</a></iframe><p>And there is also an experimental <a href="https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#aslivedata">asLiveData()</a> extension function to automatically convert a Flow into a LiveData.</p><p>Asynchronous programming is challenging in any programming language, but both RxJava and Kotlin Flow do an awesome job to simplify it.</p><blockquote>Both RxJava and Kotlin Flow are great choices for asynchronous development. Only you can decide which one is the right one for you and your project.</blockquote><p>RxJava is and still remains a perfectly valid approach for asynchronous development. It is stable, really powerful and especially recommended for complex use cases. However, it also has a steep learning curve.</p><p>On the other hand, Kotlin Flow has a simpler but extensible API, automatic streams cleanup and transparent back-pressure management.<br><a href="https://github.com/Kotlin/kotlinx.coroutines/blob/develop/benchmarks/src/jmh/kotlin/benchmarks/flow/scrabble/README.md">According to JetBrains, it also offers better performance.</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/529/1*lW3ARe7NGrlfPbN8KmAqZA.png" /></figure><p>And, if you’re planning to migrate your app from RxJava to Kotlin Flow, it might be useful to know that the two are actually interoperable (for more details see the <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-reactive/kotlinx.coroutines.reactive/org.reactivestreams.-publisher/as-flow.html">asFlow()</a> and <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-reactive/kotlinx.coroutines.reactive/kotlinx.coroutines.channels.-receive-channel/as-publisher.html">asPublisher()</a> extension functions).</p><p>All that said, for my personal taste, Kotlin Flow still requires too many @ExperimentalCoroutinesApi annotations to be considered for production. But, with some luck, most of them should go away in the next few months. Stay tuned! :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f7fdf2e2f81b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/makingtuenti/asynchronous-development-in-android-rxjava-vs-kotlin-flow-f7fdf2e2f81b">Asynchronous development in Android: RxJava Vs. Kotlin Flow</a> was originally published in <a href="https://medium.com/makingtuenti">Making Tuenti</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to build a scalable and flexible VoIP architecture]]></title>
            <link>https://medium.com/makingtuenti/how-to-build-a-scalable-and-flexible-voip-architecture-fbdedff99015?source=rss-5213b4b41f52------2</link>
            <guid isPermaLink="false">https://medium.com/p/fbdedff99015</guid>
            <category><![CDATA[voip]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[webrtc]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Paolo Rovelli]]></dc:creator>
            <pubDate>Fri, 22 Dec 2017 08:39:07 GMT</pubDate>
            <atom:updated>2019-12-27T16:49:05.339Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sHhYRE0GU6FQQbgel-6JzQ.jpeg" /></figure><p>At Calling, our job is enriching customers’ communications by providing innovative call services through the cloud. And we do it end to end.</p><p>We spend a lot of our time every quarter trying to improve the overall call experience. We want our customers to have a fluid and engaging experience when they make calls, whether it’s pure VoIP (<em>app2app</em>) or bridging from VoIP to GSM (<a href="https://www.tuenti.es/ayuda/que-es-vozdigital"><em>VozDigital</em></a>).</p><p>However, from time to time, we shift our focus slightly by taking a look at the state-of-the-art of our VoIP infrastructure or seeing if we can create a new killer-feature on top of it. And we managed to do just that when we developed Proofs of Concept (PoCs) of features like <em>call saving</em> or <em>call filters</em>, that went on to become full fledged products released into the wild. And we did it again last quarter, when we decided it was time to work on a PoC for <em>video calls</em> in Tuenti.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MXwqfMM-PxrwELG2-HqkXA.png" /></figure><p>As usual, it started with some market research that showed us that a considerable percentage of all calls over IP (using either mobile data or WiFi) were actually video and, what’s more, that our customers really wanted video calls, badly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h-6IAd17_J81gIGxLZxwuw.png" /></figure><p>Afterwards, with that data in hand, we started talking about architecture. We should also say we took this opportunity to look into what we wanted our VoIP architecture to look like in the next few years.</p><p>Thus, we ended up deciding to keep the video call PoC completely decoupled from the VoIP calls. This means that, for the time being, VoIP and video calls have different and completely separate architectures.</p><p><strong>With the new architecture, we decided at the outset to keep all the logic as well as the current call state server-side.</strong> The client-side logic only handles what’s actually local to the device, such as things like permissions management, minimum required network verification and ongoing GSM calls monitoring. <strong>This way we kept the common logic shared among our (Android, iOS and Web) clients in a single place</strong>, without the need of waiting the release cycles of clients to update it if needed. <strong>The result is a “dumb” client </strong>that just sends actions to and receives state changes from our back-end.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/359/1*pp8LWw_b71sTfuuuBKHo7g.jpeg" /></figure><p><strong>On the client-side, we split the core of our video call implementation into two main components, one called <em>“VideoPhone”</em> and the other called <em>“VideoClient”</em>.</strong></p><p>The <em>VideoPhone</em> can be seen like a physical phone device you might have on your desk. All the logic and high-level steps needed for making and receiving calls happen there. However, this knows nothing about what’s actually going on underneath. On the other hand, the <em>VideoClient</em> can be seen like the cables that go from the phone device to the wall. An that’s where all the technical details and low-level steps specifically involved with signalling and the media transport implementation are.</p><p>This architecture gives us a lot of flexibility, because the business logic does not depend on the actual signalling and media transport implementation we’re using under the hood. <em>In other words, we’ve made it possible to change the cables without touching the phone device.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/1*1wtRFVcddOadwqWy0HqLBw.png" /></figure><p>With that in mind, and since we wanted a working PoC up and running with a minimum of time and resources, we decided to initially opt for the <a href="https://tokbox.com/">OpenTok</a> video platform from <a href="https://tokbox.com/">TokBok</a> as our media transport implementation.</p><p>As said, thanks to our architecture, it’s easy to change the signalling and media stack if we need to. For example, if tomorrow we decide to replace OpenTok with our own VozDigital <a href="https://webrtc.org/">WebRTC-based</a> stack, we could do it without having to change any of the business or presentation logic above it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/738/1*GNXzIOB0_Smy_DHAr3gZHg.png" /></figure><p><strong>On server-side, we created a micro-service that handles the call state transitions.</strong> When a client sends an action, the micro-service retrieves the current state of the call and perform a corresponding use case, which depends on the action sent and the current call state. If the desired action can be performed the micro-service will update the call state and broadcast the new state to all the peers of the video call, otherwise an alert will be raised.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*1QUPPhxg_9i3SxAvnEwviQ.png" /></figure><p>For now, we’ve chosen to store the current state of the calls in Memcached. This way it can be shared among several instances of the same micro-service, therefore achieving horizontal scalability.</p><p>Whenever a call state change happens, like a peer answering a call or enabling/disabling audio or video, the server notifies all the peers by sending a <em>state-update</em> signal to the clients.</p><p><strong>Clients only receive <em>state-update</em> signals. They don’t emit them, ever. </strong>However, they do store in a cache the last call state received, so that it can be replayed to the presentation layer at any time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/688/1*m0irsxyOgCjzz3osrgDm6w.png" /></figure><p>Here it follows an high-level, rough example were Alice makes a video call to Bob who answers it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*D4d79bqLFXmmZ-pMJ_gEVA.png" /></figure><p>Finally, when server sends the last <em>state-update, </em>which is the <em>End</em> call event, this also includes details like the terminate reason (e.g. <em>Success</em>, <em>Busy</em>, <em>Cancel</em>, <em>Decline</em>, …) and the corresponding actions available to the user (e.g. retry call, offer GSM fallback, offer chat/sms message or offer to rate the call).</p><p><strong>Here again, clients just present what the server tells them.</strong></p><p>In conclusion, we’re still experimenting and testing the video call PoC. We’re working on making sure we made the right architecture choices and validating the integration of video calls with the rest of the app’s features. But, so far, the results are looking pretty good… ;)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fbdedff99015" width="1" height="1" alt=""><hr><p><a href="https://medium.com/makingtuenti/how-to-build-a-scalable-and-flexible-voip-architecture-fbdedff99015">How to build a scalable and flexible VoIP architecture</a> was originally published in <a href="https://medium.com/makingtuenti">Making Tuenti</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Developing applications for FreeSWITCH]]></title>
            <link>https://medium.com/makingtuenti/developing-applications-for-freeswitch-fccbe75ada81?source=rss-5213b4b41f52------2</link>
            <guid isPermaLink="false">https://medium.com/p/fccbe75ada81</guid>
            <category><![CDATA[voip-services]]></category>
            <category><![CDATA[freeswitch]]></category>
            <category><![CDATA[voip]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Paolo Rovelli]]></dc:creator>
            <pubDate>Tue, 25 Apr 2017 08:50:36 GMT</pubDate>
            <atom:updated>2017-04-25T08:50:36.246Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RbE33E4WqIBFHeGzBp_Xyg.jpeg" /><figcaption>Tuenti developers at work</figcaption></figure><p>Have you ever wondered how VoIP calls work in Tuenti? Let’s find out a bit more about it.</p><p>At Tuenti we use FreeSWITCH, an open-source softswitch, for both VozDigital and App2App calls. If you don’t know what a softswitch is, you can think of it as the core of a telecommunications network. It basically connects the two legs (caller and callee) of a VoIP call.</p><blockquote>FreeSWITCH is a scalable open source cross-platform telephony platform designed to route and interconnect popular communication protocols using audio, video, text or any other form of media. (<a href="https://www.freeswitch.org/">https://www.freeswitch.org</a>)</blockquote><p>FreeSWITCH provides an API that exposes primitives for call control and IVR (Interactive Voice Response) functionality. Applications can access the API using Event Socket, an Internet socket-based communications interface.<br>Event Socket Library (ESL) is available for several different programming languages, including Java, JavaScript, Perl, PHP, Python and Ruby.</p><p>In this post, I&#39;ll talk about how to use the Event Socket Library to create a simple FreeSWITCH-based application that listens to a series of VoIP call events and eventually executes commands.<br>For this demo application I decided to use Node.js.</p><p>The first step is to create an empty Node.js application and add the Event Socket Library dependency in the <em>package.json. </em>For this demo I&#39;ll use <em>modesl</em>, which is one of the various ESL implementations available.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee4009e11ead2b768a80ca2f2de3efea/href">https://medium.com/media/ee4009e11ead2b768a80ca2f2de3efea/href</a></iframe><p>Now, you need to establish an Event Socket connection with FreeSWITCH. This can be done using the<em> Connection(…) </em>constructor of <em>modesl</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/13b284e96003d4bd9da96b076084ff3d/href">https://medium.com/media/13b284e96003d4bd9da96b076084ff3d/href</a></iframe><p>Where FreeswitchConfig contains the FreeSWITCH IP, port and password. <br>For example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5641a132cf31ed038473b38db5bd66e1/href">https://medium.com/media/5641a132cf31ed038473b38db5bd66e1/href</a></iframe><p>Once you get a successful Event Socket connection, you need to subscribe to the FreeSWITCH events you&#39;re interested in. You can use the <em>subscribe(…) </em>method of <em>Connection</em> for this. Afterwards, you should start receiving events.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b15efa0c7ad3e105bc34dc101bf774c/href">https://medium.com/media/8b15efa0c7ad3e105bc34dc101bf774c/href</a></iframe><p>In the example above, you&#39;re subscribing to <em>all</em> FreeSWITCH events. However, you can also subscribe to only a particular kind of event.<br>For example, if you&#39;re only interested in <strong>DTMF</strong> (Dual-Tone Multi-Frequency) events, you can subscribe just to those (see code example below).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3cd16bd634b4df1e0bd8abb25b2155eb/href">https://medium.com/media/3cd16bd634b4df1e0bd8abb25b2155eb/href</a></iframe><p>The received events have several fields (or headers). Some fields are included in all events, such as the <em>Event-Name</em> or the <em>Event-Date-Timestamp</em>, while others depend on the event type. For example, most call-related CHANNEL events include the <em>Answer-State</em> (e.g. ringing, answered, hangup, …) and the <em>Call-Direction</em> (e.g. outbound or inbound).<br>A complete list of events with their fields can be found at <a href="https://freeswitch.org/confluence/display/FREESWITCH/Event+List">https://freeswitch.org/confluence/display/FREESWITCH/Event+List</a></p><p>You can get an event field using the<em> getHeader(…) </em>method of <em>Event</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c4f65c4e87de014fd7114a9f0a5e1a8/href">https://medium.com/media/9c4f65c4e87de014fd7114a9f0a5e1a8/href</a></iframe><p>You can now connect to FreeSWITCH, receive events and read the fields.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0fd7065815ff9e2385d37a5cdafcd7ad/href">https://medium.com/media/0fd7065815ff9e2385d37a5cdafcd7ad/href</a></iframe><p>However, you still don&#39;t have a way to execute commands in FreeSWITCH. <em>Connection</em> has two methods for executing commands: <em>api</em> and <em>bgapi</em>. The main difference is that <em>api</em> executes commands synchronously whilst <em>bgapi</em> executes them asynchronously.<br>For this demo, all commands should be executed asynchronously using <em>bgapi</em> to avoid making the service unresponsive.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f21f92ff66d627283f8ee19d8cfea88c/href">https://medium.com/media/f21f92ff66d627283f8ee19d8cfea88c/href</a></iframe><p>You can now easily create simple use cases.<br>For example, you can use the <em>uuid_getvar</em> FreeSWITCH command to get channel variables.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ae138a226690f665c296417297b0c18/href">https://medium.com/media/3ae138a226690f665c296417297b0c18/href</a></iframe><p>There is a list of basic FreeSWITCH commands at <a href="https://freeswitch.org/confluence/display/FREESWITCH/mod_commands">https://freeswitch.org/confluence/display/FREESWITCH/mod_commands</a><br>The commands you have available may depend on the modules loaded in your FreeSWITCH.</p><p>So, now you have a working demo application that listens to FreeSWITCH events and executes commands. But if you&#39;re wondering, you can do a lot more with the Event Socket Library. It can also be used for executing applications over a channel, generating events from the ESL-side and a lot more! ;)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fccbe75ada81" width="1" height="1" alt=""><hr><p><a href="https://medium.com/makingtuenti/developing-applications-for-freeswitch-fccbe75ada81">Developing applications for FreeSWITCH</a> was originally published in <a href="https://medium.com/makingtuenti">Making Tuenti</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>