Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX and Client-Server Architecture with JavaScript

I have to program websites, but I rather don't like the static HTML nature. I prefer more of a client-server architecture.

Now I've figured, that with XMLhttp, you can basically dynamically update your page and send/request for information/action to/from a server. So this would basically cover the client area.

But to complete a client-server architecture, it is necessary for the server to send/request information, too, without being queried.

Is there any way, for example for a chat server, to send back a received message to all clients (the clients use a web browser) without that the clients have to query in a fixed interval? I want to implement that one can see while you type something in.

like image 875
Stefan Steiger Avatar asked Jan 15 '10 18:01

Stefan Steiger


3 Answers

There are several different ways to accomplish this. Some of them are already answered here, but I wanted to include a few more as well as my thoughts on them.

1. Polling

Frequent requests are made to the server to check for new info. This is the worst way to do this, but probably the easiest. If your site will have a low number of users, it might be worth doing it this way.

This can be accomplished by using setInterval(myFunction, n) in javascript to send XMLHttpRequests to the server every n milliseconds. Then, on the server, you respond to this with your new info, when you have it, or some message that implies no new info.

2. Long Polling

When the page is loaded, it makes a request to the server for new info. The server holds the connection open until there is something to send back. This method reduces the amount of network traffic used, but increases the resources used on the server. You can use this for a small number of users, but it doesn't scale very well.

The easiest way to do this is to have the page that handles the AJAX request simply wait for new information to be available, then respond. This can tie up a lot connections on your server. So, use with care.

3. COMET

COMET is basically long polling, but the server is setup properly for it. It knows that these connections aren't "real" and it uses less resources to handle them. This is a great solution for this problem, but it requires that the server is explicitly setup for this purpose. There are COMET servers and COMET addins for other popular servers, but it will require some setup and sometimes some money.

Implementing this on .NET isn't the easiest thing in the world. You can pay for solutions, try to find someone else's code that does something similar, or try to write it yourself. I've not found any decent free solutions for this. If someone else has, please comment.

4. RIA

Another solution would be to include Flash, Silverlight, or Java Applet on your page. People often do this by using a 1x1 object so that they can use Flash or Silverlight to talk to the server. If you don't mind adding the dependency, this is a decent solution. If you already know Silverlight or Flash, it could be relatively simple to implement.

You can find tutorials on the internet for each of these options.

5. Web Sockets

If you are on the cutting edge, you can look into Web Sockets. It's only available in the latest builds of modern browsers. It was part of HTML5, but it might be its own spec now. Regardless, it means that older browsers won't be able to handle it. But, if you don't mind limiting yourself to the latest of browsers, you can use this amazing feature.

I believe that Chromium is the only browser that currently supports it. However, there is work being done to implement this in Firefox and WebKit.

I'll spare you the controversy and simply say that this does exactly what you want it to. The Abstract of the spec says it all.

This specification defines an API that enables Web pages to use the Web Sockets protocol for two-way communication with a remote host.

Special Mention

If you are interested in the world of Node JS, you can't go wrong with Socket IO. It will implement the best of whichever technology is available to the browser.

Conclusion

The best option is Socket.IO on Node JS. However, for an ASP.Net solution, go for COMET or Web Sockets, if you can. Otherwise, using Flash/Silverlight isn't terrible. Finally, polling and long polling should be last resorts. You could always support one of these, then fall back to another if there isn't support for it in the client's browser.

like image 137
EndangeredMassa Avatar answered Sep 18 '22 15:09

EndangeredMassa


Yes, you can use COMET.

like image 38
D'Arcy Rittich Avatar answered Sep 20 '22 15:09

D'Arcy Rittich


The client has to tell the server when the client-user begins typing. You've got a couple options here.

  1. Frequent requests from the server for the latest activity. This would be taking place for each user involved in the chat. The same request could be used to send user-specific activity to the server as well: "Jonathan is typing..."

  2. Long-polling. This essentially requests information from the server, and the server keeps the connection opened until it has something to send back. So your requests are minimized, but your connections stay opened longer.

Depending on your traffic, type of data being transmitted, server-environment, and many other factors, one of these options may shine more than the other.

like image 38
Sampson Avatar answered Sep 18 '22 15:09

Sampson