3

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?

Community
  • 1
  • 1
Abdelouahab Pp
  • 4,252
  • 11
  • 42
  • 65
  • 2
    why javascript? when was the last time you could write a `for()` loop in plain HTML? when was the last time you could define an array and do some push/pop operations on that array in HTML? – Marc B Dec 03 '12 at 14:59
  • 1
    i think that those are handled using server side language, and then converted to plain html? – Abdelouahab Pp Dec 03 '12 at 15:05
  • 2
    no. exactly how could a client browser EVER request something form a server (other than by a user clicking on a link or submitting a form) WITHOUT javascript? – Marc B Dec 03 '12 at 15:07
  • am sorry but am beginner, from what i understood in the past: javascript is for programs that frees server, and python (for ex)is to do heavy work on the server. and my image of internet was simple request-response, and when read about websocket i tried to read the tornado example and dident found where the stuff happened, dident know that i must read the javascript file too! – Abdelouahab Pp Dec 03 '12 at 15:11
  • 2
    with exceptions (like node.js setups), javascript runs in a client browser, and essentially has nothing to do with the server. except getting loaded from that server and being able to talk back to it. – Marc B Dec 03 '12 at 15:13
  • here is the example i always put in mind to understand JS: the drop-down list, when i change an element another will change,this is better done using javascript (client) than server side, or for example a visual keyboard. but dident know that it can affect a technology (websocket) – Abdelouahab Pp Dec 03 '12 at 15:16

2 Answers2

4

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.

kanaka
  • 70,845
  • 23
  • 144
  • 140
Axel
  • 867
  • 6
  • 9
  • thank you, i'll try to learn it using tornado, it has websocket, and it seems that there are libraries used with tornado to simlify websocket? – Abdelouahab Pp Dec 04 '12 at 10:02
  • 1
    It looks like Tornado has a basic webSocket class handler: tornado.websocket.WebSocketHandler, so that's where you would want to start. You'll have to look around if anyone has implemented any protocol library on the server side. For example, if you find a server-side implementation of stomp, then you just need to look for a JS stomp library for the browser side, and respective libraries for iOS and other platforms. Of course, if you just want to do simple server push, then the basic API is probably enough, but you can do so much more with good protocols. – Axel Dec 04 '12 at 18:55
  • found this https://github.com/MrJoes/tornadio2 i think it will simplify the work? – Abdelouahab Pp Dec 05 '12 at 10:20
3

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.
kanaka
  • 70,845
  • 23
  • 144
  • 140