4

Be aware of what I ask, I know how to write and run a WebSocket server.

My question is, I have been using Ajax to connect server for a very long time, because I use jQuery, it provides a $.ajax() method that is easy to use.

But now, my website can run in WebSocket mode, it means everything I can do by using Ajax, is now available by using WebSocket.

But I don't know if there is a way to connect the server just easy as $.ajax(), which can help me to make a change from Ajax into WebSocket without too much effort.

Anil
  • 3,722
  • 2
  • 24
  • 49
A-yon Lee
  • 2,074
  • 2
  • 9
  • 18
  • Use some thing like https://socket.io – lintu Mar 17 '17 at 10:10
  • 1
    you may like to refer http://stackoverflow.com/questions/10377384/why-use-ajax-when-websockets-is-available – Anil Mar 17 '17 at 10:11
  • 1
    Since they are fundamentally different protocols, there's probably no trivial transition. You can reimplement all the functionality in websockets and create functions which act exactly the same as the `$.ajax` counterparts so far, then replace all function calls to `$.ajax` with your new functions… But really: why? One of the advantages of Ajax is that requests can easily be cached and do not hog server resources for open connections. If you don't need bidirection communication (and apparently you don't), then why replace it at all? – deceze Mar 17 '17 at 10:25

1 Answers1

0

Today WebSockets can be used where we are using AJAX, however objectives of both were different

AJAX/XHR was for a web page to asynchronously fetch/send the resource/data.

WebSockets were for other purpose, the objective here was to open a continuous and duplex communication between two entities, for exchanging messages.

WebSockets Tools

µWebSockets: Highly scalable WebSocket server and client implementation for C++11 and Node.js.

Socket.IO: A long polling/WebSocket based third party transfer protocol for Node.js.

WebSocket-Node: A WebSocket server API implementation for Node.js.

Total.js: Web application framework for Node.js (Example: WebSocket chat) Faye: A WebSocket (two-ways connections) and EventSource (one-way connections) for Node.js Server and Client.

SignalR: SignalR will use WebSockets under the covers when it's available, and gracefully fallback to other techniques and technologies when it isn't, while your application code stays the same.

Caddy: A web server capable of proxying arbitrary commands (stdin/stdout) as a websocket.

Anil
  • 3,722
  • 2
  • 24
  • 49