Download Jade & # 8594; HTML via Websockets + NodeJS instead of AJAX

It is surprising if there is a clean, correct way to download html markup via websites instead of requesting $ .ajax ({...}). I'm somewhat new to websockets, so I'm trying to figure out where it can completely replace AJAX and so on ...

Right now I just create another "post" router in my NodeJS application to download html, but I don't know if all this is even necessary.

+4
source share
2 answers

you need websites mainly if you want to maintain a bi-directional connection between client and server, useful for real-time applications (e.g. chats, stock marketing, e-learning, etc.).

if you need to load the html tag down, you don’t need to switch from the client to the server many times to download the content and serve it, it will be very elegant and not wasteful.

You can also use ajax requests to get the route and $ .get if you do not want to transfer additional payload to the server.

0
source

Of course, you can transfer data via web ports for your client from the Node.js server and, as soon as on the client, just send it to the page.

If you use socket.io, for example, you can fire an event inside your server using the generated html, which will be received in the client code:

On server:

socket.emit('yourFiringEvent', variableContainingYourRawHtml); 

On javascript client:

 socket.on('yourFiringEvent', function(htmlResult) { $("#yourContainerId").html(htmlResult); //jQuery flavour ;-) }); 

When your client code receives an event from the server, it will load the data into theContainingYourRawHtml variable inside the HtmlResult

If you do not use it, I recommend using the socket.io library to use websocket, it is quite simple and convenient:

http://socket.io/

0
source

Source: https://habr.com/ru/post/1436997/


All Articles