JavaScript — Event Loop & Concurrency

In this article we will try to figure out how event loop and concurrency actually works in javascript.

Javascript, a single-threaded functional language.

One thread == One call stack == One thing at a time

Have you ever wondered that despite of being single-threaded functional language, how javascript handles our callback? During execution, where does our callbacks go and how they come back in our call stack after certain amount of time? So, lets try to figure out how javascript executes the functions internally.

We will start with a simple function execution. Let’s assume we have the following code snippet, so when we execute it, internally javascript engine creates three main components during run time.

function a(){
 console.log(“a”);
 b();
}
function b(){
 console.log(“b”);
}
a();

Call Stack — It is the main thread by which execution of our program takes place. During run time, engine creates frames, which contains arguments and variables of that function (not objects) and push them into call stack. main is our entry frame by which execution starts, after that it will create a frame for a and push it into call stack. Inside a we are calling b, so it will create a frame for b and push it into call stack. When execution of any function finishes, it will pop corresponding frame from the call stack.

Object Heap — It is nothing but a memory space where objects resides.

Message Queue — During execution, javascript engine (V8) also maintains a message queue. Whenever the browser finds out that timer for a particular callback is completed it pushes that function to the message queue. It is a pipe which browser uses for sending their event to the main execution call stack for execution. So message queue is nothing but collection of callbacks those are waiting for the execution.

Now, for some more clarity, let’s extend our example and add some callbacks. We will use ‘web api setTimeout’ to add function aa and bb inside function a and b respectively with timeout 3000ms and 100ms.

setTimeout is a web API which is handled by the browser concurrently. You can call web API’s and they will keep working on them in separate thread.

function a() {
console.log(“a”);
setTimeout(function aa() {
console.log(“aa”);
}, 3000);
b();
}

function b() {
setTimeout(function bb() {
console.log(“bb”);
},100);
console.log(“b”);
}

a();

Starting with main frame, engine will create frame for function a and push it to call stack and browser start timer for function aa. After that, engine creates frame for function b and pushed it into call stack. Similar as aa, for function bb also, browser kicks a timer for it.

On the one hand, the call stack execution is going on and it pops frames from call stack as ba and then main, and on the other hand at the same time callback timer is ticking. Whichever callback timer completes its duration first (regardless which started first), it will move to the message queue. So, in this case, even though timer for aa started first it is taking more time then bb so in message queue, bb moves before the aa.

Now significance of Event Loop comes in picture.

Event loop is continuously ticking during the whole process and watching the call stack and message queue. As soon as it finds out that all frames of call stack (a, b and main) are popped and call stack is empty, it started creating frames for callback functions (bb, aa) and pushing them into call stack from message queue. Here you can see event loop waited for the completion of previous call stack before pushing message queue frames to them. It means whatever time we provide in the callback, that is the minimum time which it will take to execute.

Conclusion

So the crux is, even though Javascript is single-threaded, our browser and node provides us the API’s which we can call for concurrency execution and event loop will help in pipelining those execution results from message queue to the call stack.


Screen Shot 2016-05-12 at 3.57.51 PM

Customizable Web SDK for Chat-Messaging

 It was our customer’s use cases that encouraged us to write this article. We are going to highlight our Web SDK abilities and use cases that you can leverage with Applozic platform.

We introduced our Client API’s and Native SDK’s in Sep’2015, enabling developers to build cross-platform messaging for mobile, web and desktop. Since then, we have seen our platform being used in 100+ businesses but the most amazing part is that each of these implementations are unique in its own way. Each business has their own use cases and UI customizations, some with lengthy work flow and some even more complicated. Our highly simplified API’s and UI toolkit made it all an amazing experience.

Our Web SDK enables a variety of cross-platform messaging use cases:​

Match making and Dating services

On-demand delivery services

Community building and group chat

Online events and Social networking

Cross device messaging for Delivery – Recipient, Customer – Agents

Marketplaces connecting buyers and sellers

Be it Bubble, SideBox or Full view, we have you ready to use UI: https://www.applozic.com/docs/web-chat-plugin.html#customization

fullview

sidebox

We offer you the freedom to build a great messaging experience in any product. Our Web SDK will keep evolving with our extensive roadmap but your feedback and suggestions would always help us reach new highs.

The documentation is available as part of our developer portal, along with example apps: https://www.applozic.com/docs/web-chat-plugin.html#first-level

Sign up for Applozic developer account and get started: https://www.applozic.com/signup.html 

If you have any questions about integration and customization, please connect with us over live chat and we will be happy to assist you.

 


Buyer-Seller

Why in-app messaging is the best way to engage users today?

Instant messaging, a term that entered common usage in the early 1990s, but the concept actually dates back to the mid-1960s. Multi-user operating systems such as the Compatible Time-Sharing System (CTSS),created at Massachusetts Institute of Technology (MIT)’s Computation Center in 1961, allowed up to 30 users to log in at the same time and send messages to each other. The system, which is perhaps closer to what we now think of as email, had hundreds of registered users from MIT and other New England colleges by 1965.

Since then, we have come a long way as today we have multiple ways to communicate. In case of buyer – seller interaction, we have options like push notification, in-app messaging, live chat and real time chat. In case, you want to understand the difference please read, “Difference between Real Time Chat, In-App Messages, Live Chat and Push Notifications”.

Let us now understand why in-app messaging seems to be the most effective way of buyer- seller interaction. But before that, let me tell you what in-app messaging actually is?
In-app messaging (also known as in-app communication) is the act of messaging, communicating with users through a software application, either on a mobile phone or tablet device or through software on a PC.”

By now, you must be thinking,”so what’s the big deal about in-app messaging?

                    Question-Mark-III-2
(Source:http://goo.gl/aptxtC)

Before we dive deeper into the capabilities of in-app messaging, let us draw some understanding from a recent report released by Experian Marketing Solutions.
The findings of the report highlighted, “the top initiative for 2016 will be inclusion of in-app messaging”. In fact, they estimated that 44% of marketers will focus on installation of chat option in their application followed by 39% focussing on sms and 38% on push notifications.

app marketing initiatives(Source: http://goo.gl/XJir7z)

This study affirms that in-app messaging is on the cusp of widespread adoption, and is primed to create more targeted, purposeful user sessions.

So,  why is everyone after in-app messaging? Let me tell you why

  • In-app message has a higher reach when compared to push notification as it gets rid of the headache of seeking customers consent before sending.
    In case of push notification, a customer is expected to subscribe to it but only 45% of customers do that.
    So what are you loosing?
    A way to engage majority of your users (i.e 55%) and most importantly, a source to enhance your revenue.

On the other hand, in-app messaging comes with no constraint as users are guaranteed to receive notification leading to 8X higher response rate than push notifications.

  • In-app messaging is estimated to increase the app launches by 27% and magnify the engagement rate by 3X!
    It is because in-app messaging allows your customers to move from the grid of users to active customers through higher engagement and customized alerts.The probability of uninstalling your app within 30 days of downloading gets reduced,leading to an increase conversion rate up to 4X.

Buyer-Seller
Source: Screen-shot of a buyer seller interaction of our client

  • In-app messaging allows you to cater to the customers who already are your users. Thus, it helps marketers to create campaign which are highly targeted for existing users base. On the contrary, push notifications or SMS are often considered as source of invasion. In fact, lot of users have said, that they feel harassed as they continue to receive alerts even after the requirements are met.

IP messaging is defined as communications channels that rely on mobile TCP/IP data links for delivery rather than the traditional telephony network. IP messaging allows business to have a complete picture about messages they send. They can easily track if the messages were delivered/not delivered/read/not read.IP Messaging can help business to focus on core activities by reducing investment in marketing and operational activities. The time and cost spent on IP messaging is minimal when compared to sending SMS to that many offline users.

However, we are also of the opinion that marketers are yet to exploit the possibilities of in-app messaging as the segment is still in nascent stage. In-app messaging is mainly being used to broadcast generic messages to the entire user base leading to higher churn rates as users considers them to be spammy.

Applozic aims to help marketers to create customized campaign for complete communication solution. We help our users to have 1-to- 1, 1-to- many, group chat, context based chat with audio/video calling and sms/email fallback support.Also,we have designed a dashboard with rich insights and analytics to help marketers strategize their campaigns.

We aim to create an environment in which our users don’t be a victim of mobile engagement crisis and focus on using in-app messaging tools to combat churn and enhance user experiences.