Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With websockets, is there a place for AJAX?

I'm currently building a realtime application using Node. I'm using socket.io to power my real-time interactions, but have jQuery loaded, so I have AJAX available to me. I initially used socket.io for all my communication between the server and client.

I'm starting to think that AJAX might be better suited for certain cases like doing RESTful transactions asynchronously, because I don't have to write a separate message case in my socket to handle each new transaction as well as write the RESTful routing.

I'm wondering if I am on to something or if its best to use sockets for performance or something else I'm not thinking about.

Thanks! Matt Mueller

like image 941
Matt Avatar asked Jan 12 '11 08:01

Matt


People also ask

Does AJAX use WebSocket?

Ajax uses the HTTP Protocol and can send requests using POST/GET methods from Client to Server. WebSocket is itself a protocol to communicate between Client and Server, distinct from HTTP. In Ajax when you send a request , server sends response for that request and connection ends.

Can WebSocket replace AJAX?

WebSockets isn't intended to replace AJAX and is not strictly even a replacement for Comet/long-poll (although there are many cases where this makes sense). The purpose of WebSockets is to provide a low-latency, bi-directional, full-duplex and long-running connection between a browser and server.

Does socket IO use AJAX?

SocketIO is built on top of the WebSocket protocol (RFC 6455). It was designed to replace AJAX entirely. It does not have scalability issues what-so-ever.

When should you not use a WebSocket?

Avoid using WebSockets if only a small number of messages will be sent or if the messaging is very infrequent. Unless the client must quickly receive or act upon updates, maintaining the open connection may be an unnecessary waste of resources.


1 Answers

Yes, WebSockets (RFC 6455) and Ajax are quite different and serve different purposes.

As you say, with Ajax you can do RESTful requests. This means that you can take advantage of existing HTTP-infrastructure like e.g. proxies to cache requests and use conditional get requests. Ajax request may be quite heavy-weight since every Ajax request contains HTTP headers and include cookies.

WebSockets is designed for low latency bi-directional communication. By design, WebSockets has very little overhead in each message. E.g. WebSockets messages doesn't have to include any HTTP Headers, and may in future be used for VoIP and streaming in both directions.

Another difference is that Ajax can be used with stateless servers. E.g. if you have your web load balanced with multiple servers, any server can handle an Ajax request, even after reboot (or upgrade). Websocket's are "connected" and use a stateful server, so it may be harder to use multiple servers with it.

There is also Server Sent Events, that are similar to WebSockets, in that the server can push data to the client (which can't be done with Ajax without hacks (e.g. comet)), and it can also handle automatic reconnections. But it's only for messages in one direction (server to client). See HTML5 Server-Side Event: EventSource vs. wrapped WebSocket.

like image 182
Jonas Avatar answered Sep 24 '22 01:09

Jonas