Simultaneous client side and server side requests

Asked

Viewed 728 times

3

When using Ajax, I create an asynchronous request for my server. If I use N ajax requests simultaneously, I’m still handling these requests synchronously on the server?

If yes, to create an asynchronous request on both client-side and server-side, I would need to use threaded programming on the server?

What are the methods of creating both front-end and back-end asynchronous requests?

2 answers

3


If I use N ajax requests simultaneously, I’m still handling these requests synchronously on the server?

It depends on the server implementation. Normally, the response method has something like public async Task<>, which indicates that the response will be processed as a separate thread (i.e., scalably).

If yes, to create an asynchronous request on both client-side and server-side, I would need to use threaded programming on the server?

Exactly.

What are the methods of creating both front-end and backend asynchronous requests?

Assuming your application is ASP.NET MVC, using Web API 2 or not, the prototype method looks something like this:

public async Task<IEnumerable<Noticia>> GetNoticias()
{
    return await db.Noticias.ToListAsync<Noticia>();
}
  • async indicates that the method is run asynchronously;
  • Task serves to indicate to the object that will manipulate your thread what kind of return will be made;
  • await suspend thread until return is assigned. Thread suspension does not interrupt other streams that are running;
  • As I had imagined. So in a method that does not use Async is not asynchronous? If I make N requests for a non-async method, it will be dealt with in the main thread and will have its return synchronously, correct?

  • @Vinícius Exatamente.

  • @Vinícius I would really like to know your need. Is it for study purposes? Is it for scalability purposes? What is the purpose?

  • @Guilhermeoderdenge, There is a page in one of the systems I use that makes more than 50 Ajax requests to the server. I suspected that regardless of the amount of requisitions, it was being executed synchronously, as Gypsy clarified. The application is in webforms.

  • @Does running synchronously make any difference to you? I ask this because 50 asynchronous requests to the server should not affect applicability. Who knows the performance, but not the applicability - your language and the server will know what to do when simultaneous requests are made. Note: simultaneity is different from competition.

  • @Hermeoderdenge, yes. These are pings tests, so I believe they can be done simultaneously without impacting the applicability.

  • @Vinicius You want to run the tests simultaneously or you do things in parallel with these tests, such as logging in a log?

  • @Guiloderdenge, just test. The method that should be asynchronous just tests and returns whether this ip exists or not. As are many ip’s, everyone’s running time is very large.

  • 1

    @Vinicius I get it. So go threads! I’ll leave my answer for knowledge purposes.

  • great @Guilhermeoderdenge, let me get one last question. If I make N ajax requests simultaneously for a web-service, will it treat each request as a 'client' or will it be queued? Taking into account that you are programming without async.

Show 5 more comments

2

Imagine the following scenario: 50 users are accessing your application simultaneously. They do things there, and some of them are client requests to the server - such requests are asynchronous.

When this happens, multiple parallel and simultaneous requests are sent to your server and it solves how to handle it.

When firing more than one request (asynchronous or not) at a time to the server, the server’s behavior is not necessarily based on a method you employed.

Let’s look at a question of yours:

[...] I would need to use threaded programming on the server?

An asynchronous process is dispersed to threads - not things that should be compared because it has relatively different purposes.

So, to make it clear: you don’t have to do at all nothingness on the server if you do not find it necessary. Your requests are made to the server through a method/verb and depending on the forwarding of an intermediary - controllers, for example -, your application should react in a specific way.

Why threads are different from asynchronous processes?

Threads give an app the ability to work with concurrent processes. A computer can surf the internet and play music simultaneously because of them: they are parallel processes that consume processing - processing that is (re)used to perform multiple tasks.

Your application is one thing only: what it does is its responsibility; if it will enter some information in the database And display a message on the screen: the problem is entirely and exclusively his - which, in turn, is a unique process.

The responsibility of providing the database for the insertion of the information and the browser that is where you will display the message is the responsibility of the operating system. Here, therefore, is where the threads. At first, you nay needs and/or will manage.

But what if I really want to use threads?

You can. Languages offer means and mechanisms for this - C#, by the way, does it very well. What I’m trying to point out is that you not necessarily accurate.

Environments and web languages are naturally equipped to handle parallel processes and you can, most of the time, invest your time focusing only on your requests; in your "AJAX".

Browser other questions tagged

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