What is the difference between asynchronous and synchronous communication?

Asked

Viewed 204,336 times

70

I’ve heard a lot about asynchronous and synchronous communication, but I don’t know what they really are.

Could someone explain to me what the difference and how each one works? When and what should I use them for? If possible with code examples.

3 answers

59


I will try to complement the answer from Lolipop. :)

Synchronous and Asynchronous communications are two distinct methods of synchronization of transmitting, each has its advantages and disadvantages.

Analogy

Let’s imagine that an emitter is sending a text message through a tube and that the message is sent using balls, each ball with a letter. When the balls reach the receiver, the message has to be reassembled, that is, you have to put the balls in the correct order, to get the message back.

  • In asynchronous communication, each ball has a sequence number, which allows it to be placed in its position. This allows the balls to be sent and received in any order, since this sequence number identifies the position of each ball (letter) in the message.
  • In synchronous communication, the balls have to be sent and received in a synchronized way, maintaining a well-defined order: the first ball (letter) to be sent, must be first received and so on.

Analogy taken from Wikipedia, another analogy can be seen here.

Synchronous Communication

In synchronous communication the transmitter and receiver are synchronized by the same clock, the receiver receives continuously (even when no bit is transmitted) the time signature data in which the sender sends them.

inserir a descrição da imagem aqui

Simple example in Javascript:

function foo(){
    console.log("foo");
}
function bar(){
    console.log("bar");
}
function baz(){
    console.log("baz");
}
foo();
bar();
baz();

These tasks shall be carried out in, bar shall not be executed until foo has finished and baz in turn will only be executed after the end of bar, there is thus a synchrony.

Asynchronous Communication

In an asynchronous communication to synchronization is different, the sender sends the data stream and periodically inserts a signal element, commonly called flag, so that it is possible to distinguish where it begins (the information at the beginning of the transmission is start-bit) and ends (the information for the purpose of transmission is stop-bit) the data block and its position following the transmitted data.

inserir a descrição da imagem aqui

Example:

function foo(){
    console.log("Executando algo assíncrono aqui");
}
function bar(){
    setTimeout(foo, 0);
    console.log("Alguma coisa por aqui");
    console.log("...");
}
bar();

Here there is not the same synchronization as in another example, one function will not wait for the end of the other. In this example we use the function setTimeout to line up foo who will execute 0 ms after the event loop, the event loop will be unavailable until you complete your current run stack - in this case, the rest of bar, which includes the two declarations console.log. After the end of bar the event loop is free, and so foo can be executed.

Exit:

Alguma coisa por aqui
...
Executando algo assíncrono aqui

Related: How asynchronous programming works in Javascript?

When and why should I use them? Much of the decision-making process on this involves common sense, a set of factors, an asynchronous communication can be used when the answer does not have to be delivered urgently, this type of communication is suitable for situations where urgent data delivery is not necessary, on the other hand, it would not be appropriate to use this type of communication if you need immediate interaction. Synchronous communication is ideal when spontaneity is needed, as in a real-time conversation between two or more people.

Both synchronous and asynchronous communication have their advantages and disadvantages, using one or the other will depend on the need for their application.

References and suggestions:

57

inserir a descrição da imagem aqui

ASYNCHRONOUS DATA COMMUNICATION

In Asynchronous Transmission, a special bit is inserted at the beginning and end of a character transmission and thus allows the receiver to understand what was actually transmitted. Imagine a sequence of data that needs to be transmitted. Each data block has a flag (control species) that informs where this block begins and ends, in addition to the position in the transmitted data sequence.

With this, the data can be transmitted in any order and it is up to the receiver to interpret this information and put it in the right place. However, the disadvantage is the misuse of the channel, as the characters are transmitted irregularly, in addition to a high overhead (the control bits that are added at the beginning and end of the character), which causes a low efficiency in data transmission.

SYNCHRONOUS DATA COMMUNICATION

In synchronous data communication, the transmitting device and the receiving device shall be in a state of sync before the communication starts and remain in sync during transmission. Imagine the same data sequence that needs to be transmitted synchronously. Each block of information is transmitted and received in a well-defined instant and known by the transmitter and receiver, that is, these have to be synchronized. When a block is sent, the receiver is locked and can only send another block when the first one is received by the receiver.

Pros and cons

Asynchronous transfers are usually faster than synchronous transfers. This is due to the fact that there is no time to coordinate the transmission. However, due to this, more errors tend to occur in asynchronous transfers. If many errors occur, this can invalidate the time saved with the initial time of setting the parameters because the receiver will have to take measures to correct the errors.

Usages

Asynchronous transfers work well in situations where the exchange takes place over a reliable physical medium, such as optical fiber or coaxial cable, for example. This helps minimize transmission errors, so time saved with setting parameters results in faster transfer from the end-user’s point of view. Synchronous transfers work well for less reliable media such as electrical wires or radio signals. Here, it is worth taking longer to coordinate the details of the transfer, because this compensates for the mistakes made in the physical environment.

Example with ajax

Running asynchronously

$(function(){
    var call = $.ajax({
        url: "controller/action",
        dataType: "json",

    }).responseText;

    alert(call);

});

This function will make the Request and continue processing, the attribute responseText will not get the result of processing, because it does not matter if it is finished or not, we are working without synchrony.

inserir a descrição da imagem aqui

Running in a synchronous way

$(function(){

    var call = $.ajax({
        async: false,
        url: "arquivo.php",
        dataType: "json",

    }).responseText;

    alert(call);
 });

This time the program will wait for the response to return to continue processing, assuming that any JSON server comes back inserir a descrição da imagem aqui

Sources:

differences-between-requisicoes-synchronous-and-asynchronous-with-ajax

data-synchronous-x-asynchronous communication

  • Better answer. No doubt...

26

For those who want to understand the difference at the conceptual level, without going into technical details, goes well a metaphor.

Synchronous communication is like a radio conversation

In a VHF radio communication, only one person can speak at a time (you need to press a button to speak, and the other side needs to release the button to listen). While one person speaks, the other only listens. One must wait for the other to finish his message in order to reply. Only one message at a time is conveyed through the communication channel, and in a single sense.

Asynchronous communication is like a phone call

Ignoring the latency of the telephone network and the time it takes for information to travel through the lines, both sides of the conversation can talk and listen at the same time. The ability to receive information and to send information simultaneously is limited only by one’s own abilities, but the communication channel is asynchronous. If I ask my interlocutor a question, I might as well ask him a second one while he’s answering the first one. He answers when he wants/can.

  • Great answer. Simple and direct. Not that the others were bad, but yours used a legal analogy to illustrate what happens in both cases.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.