Abdelouahab Pp
Abdelouahab Pp

Reputation: 4440

Why Javascript is IMPORTANT for Websocket?

It seems a bizarre question but i'm really confused, because when downloaded this example from Tornado i thought that: okey, i run it, and it will work! but the problem, it dident work, because i tested in in Offline mode, and dident have jQuery in cache!

And found this from SO:

Ajax - create connection to server send some data (simplified as get / post), and receive response.

Long poll - create connection to server, send some data, keep connection and receive sometimes from server some data. Connection is kept for short time, and does periodical reconnection. On server side it still dealt like Web Page.

WebSockets - create connection to server, and keep is as long as needed. Server or client can easily brake it. Bidirectional sending of data. WebSockets usually uses masking for each message so data is simply encrypted.

So then, why there is always Javascript under the hood even it's something related to server side?

Upvotes: 3

Views: 3115

Answers (2)

Axel
Axel

Reputation: 927

JavaScript is important to WebSocket only in browsers because the browsers have implemented the WebSocket API (See RFC 6455) in JavaScript. So if you want to access WebSocket from within an HTML5 page, you need to write JavaScript. However, you can also do WebSocket in a client in Java by using a Java Applet. (Although applets have fallen out of favor.) Additionally, it is possible to do WebSocket from native applications, including mobile iOS.

Many WebSocket server platforms try to support multiple types of clients. For example, Kaazing provides clients not only in JavaScript, but also in .NET, Silverlight, Java and Objective-C. The basic idea with WebSocket is that you write your server logic once, and you can then "harvest" what you did in various different clients.

Just keep in mind that if you are going to do WebSocket, you will need a programming language other than basic HTML tags because you need to process the data coming in over the WebSocket connection. That data can come via many different protocols, such as AMQP, STOMP, socket.IO, WAMP and many others. For each type of protocol, you will actually need a different library that can handle processing the protocol.

Upvotes: 5

kanaka
kanaka

Reputation: 73091

Javascript started off as a way to add client-side form validation and small bits of dynamic user interaction to a web page. However, modern Javascript is a very powerful language (with many annoyances too) that can run in a browser or on a server (using something like Node.js). Modern browsers provide many APIs that only apply to Javascript (unless and until another language like Dart is universally supported) such as Web Workers, Canvas, Web GL, Web Audio API, XMLHTTPRequest (i.e. AJAX), timers, events, etc, etc.

The old concept of a browser is that of a program that is able to download and render static HTML markup documents. It is now more useful to think of a browser as an operating system + libraries + APIs that is primarily for executing web applications. In other words, the primary purpose of modern web browsers is to execute Javascript and to provide APIs for Javascript. The main purpose of the HTML is to specify the initial Javascript files to load to start the web application. HTML and CSS can also be used to define the initial state of the DOM tree (the visible part of the Javascript API). However, everything you can specify statically with HTML and CSS you can also dynamically generate using Javascript.

In that context, WebSockets is an API in the browser that enables a web application (i.e. a Javascript program running in the browser) to make a low-latency bidirectional communication channel to a WebSocket server.

Update:

Why don't they just say "WebSockets are available in Javascript version x.x.x"?

  1. Javascript is not just for the web (e.g. Node.js) and these APIs are not just for Javascript. For example, if you have a version of Chrome with Dart enabled, then these same APIs are available from Dart. The script tag allows for other languages so future browsers may run other languages that would have access to these APIs.
  2. Javascript is a language defined by ECMA, WebSockets, Web Workers, etc are browser APIs that are defined by the W3C as part of HTML5. The browser version determines which web APIs are available and also the version of Javascript. You might have a browser with an older version of Javascript that supports new APIs or vice versa.

Upvotes: 3

Related Questions