DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Frameworks
  4. AJAX Over WebSockets

AJAX Over WebSockets

Developers usually pit AJAX and WebSockets against each other, making it an either or selection. We look at how to combine the two and bring peace to the universe.

Wilfred Nilsen user avatar by
Wilfred Nilsen
·
Nov. 28, 18 · Analysis
Like (12)
Save
Tweet
Share
17.14K Views

Join the DZone community and get the full member experience.

Join For Free

I have seen so many AJAX vs. WebSockets discussions that I thought it would be cool if one could combine the two of them. I might be biased, but I have always gravitated towards using WebSockets as the preferred communication mechanism. However, since AJAX is so rooted in many web developers, I thought it would be interesting to see if one could implement AJAX on top of WebSockets.

You may think AJAX and REST are the best technologies since sliced bread; however, the entire AJAX protocol can be built using Websockets technology. This makes Websockets literally a superset of AJAX so it may make sense that we use WebSockets as the main communication link.

There are several benefits to running AJAX over WebSockets, such as:

  • Less overhead, thus much faster (including less overhead than HTTP/2).
  • Much easier to implement stateful services, if needed.
  • Both AJAX and asynchronous WebSocket data can be multiplexed over the same connection.

AJAX returns a single response to a single request. However, AJAX is asynchronous; thus, a web front-end can send any number of AJAX requests without having to wait for each response.

WebSockets, on the other hand, can handle multiple bidirectional messages between the browser and the server. There is no correlation between the messages sent in either direction. It is up to the application to interpret and manage these messages. However, implementing AJAX on a WebSocket connection is conceptually very easy:

  1. The data is encoded as JSON.
  2. The JSON data is sent to the server over the WebSocket connection.
  3. The server reconstructs the data from the JSON string.
  4. The server finds the AJAX service and calls the service.
  5. Response data from the AJAX service is encoded as JSON.
  6. The server sends the JSON response data to the browser via the WebSocket connection.
  7. The AJAX response is managed in the browser.

There are a few AJAX specific features we need to think about when designing the code. As we mentioned above, AJAX is asynchronous on the client-side, thus many AJAX requests can be in-flight at the same time. Each AJAX request is associated with a JavaScript callback that gets called when the server sends the response. We must find a way to uniquely identify each in-flight request such that each AJAX response triggers the correct callback function. The following figure depicts three asynchronous and in-flight AJAX requests:

Image title

Figure 1: Three asynchronous in-flight AJAX requests

Each of the asynchronous responses A', B', and C' must trigger the correct response callback function for the request A, B, and C, even if the response messages should be in a different order.

The easiest way to implement the client side AJAX callback handling is to store each callback in an object and give each callback a unique ID. The ID, which can for example be a number, is sent to the server as part of the message. The server side then bundles this ID with the response data.

Download the AJAX Over WebSockets JavaScript Library

If you are interested in the JavaScript library we implemented, head over to the original article and download the AJAX over WebSockets JavaScript source code.

AJAX WebSocket

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • HTTP vs Messaging for Microservices Communications
  • Custom Validators in Quarkus
  • How To Choose the Right Streaming Database
  • DevOps for Developers: Continuous Integration, GitHub Actions, and Sonar Cloud

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: