Can javascript get a socket request?

How can I get the javascript client application to receive socket requests? I do not mean a response to a request, but a request. Javascript works in a browser without HTML5.

The fact is that I want my web page to reload the changed content, but without having to make a request to the server every few minutes.
I hope the server can make some javascript request on the page, which will make it a page refresh. If not what you could offer instead of javascript in this area.

+4
source share
2 answers

Many browsers that support HTML5 implement the WebSocket interface. WebSocket allows two-way communication, so the browser and server can send requests. Check this post for more information. Which browsers support the HTML5 Websocket API?

If your browser does not support WebSockets, you can try WebSocket emulation written in Flash / JS from this site https://github.com/gimite/web-socket-js

If this is also not suitable for you, then the โ€œlong-term surveyโ€ is the last option. In this case, the browser requests the server for some data, and if the server does not have any information available for the browser, it does not send an empty response. It contains a query and expects new data. After receiving new data, the browser immediately requests the server. Check out these links for more information: http://en.wikipedia.org/wiki/Push_technology http://en.wikipedia.org/wiki/Comet_ (programming)

+4
source

Yes and no.

No

Javascript running in a browser environment cannot listen on sockets because it runs in a sandboxed environment with limited capabilities.

Yes

However, JS is a full-fledged programming language, so if it works in an environment where it is not crippled, yes, it can do this and much more.

A good example is node.js - http://nodejs.org/

Wiki Page - http://en.wikipedia.org/wiki/JavaScript#Uses_outside_web_pages

+4
source

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


All Articles