SlideShare a Scribd company logo
Web Socket
Client/Server communications
Polling Vs LongPolling Vs Streaming
HTTP half-duplex
HTTP Overhead-less efficient communication
Polling Techniques, Ajax, protocol Switching from Http to Websocket standard (RFC-6455), javascript engine for java (Nashorn) were discussed in presentation.
To create desktop-like applications
in the browser
● Developers used hacks in servers to keep connections open longer
● Open up resource allocation issues on servers
● Perceived latency to the end-user may be low, but
● Polling – unnecessary requests and stream of opening and closing
connections
● No facility for layering other protocols on top of Comet or AJAX
● WebSocket gives you a bi-directional full-duplex communications
channel that operates over HTTP through a single socket.
WebSockets give you the...
● Ability to utilize an upgraded HTTP request
● Sends data in a message-based way similar to
UDP
● Has the reliability of TCP.
● Single-connection, negligible penalty in
resource uti‐lization, layer another protocol on
top of WebSocket, a secure way over TLS
WebSocket is an...
● Event-driven, full duplex, asynchronous communications channel with single TCP
Connection
● Primary benefit is reducing resource needs on both the client and more importantly,
the server.
● IETF standardized as RFC-6455
● Utilizes HTTP as the initial transport mechanism
● Doesn’t end after a response is received by the client
● The client and server can freely send messages asynchronously without polling for
anything new.
● Initialize the browser native WebSocket object
var ws = new WebSocket("ws://localhost:8181", protocol (optional));
● Sub-protocols so a single server can implement multiple WebSocket sub-protocols.
(Registerd, open(XMPP, STOMP), custom)
Events
●
Open
ws.onopen = function(e) {
console.log("Connection established");
ws.send(JSON.stringify(stock_request));
};
●
Message
ws.onmessage = function(e) {
var _sData = JSON.parse(e.data); //can close connection
};
●
Error
ws.onerror = function(e) {
console.log("WebSocket failure, error", e);
handleErrors(e);
};
●
PING / PONG
– PING frames get sent out by the server only
– Browser implementations should send back PONG frames in response
●
Close
ws.onclose = function(e) {
console.log(e.reason + " " + e.code);
};
Methods
● Send
var ws = new WebSocket("ws://localhost:8181");
ws.onopen = function(e) {
ws.send(JSON.stringify(_request));
}
● Close
ws.close(1000, "Goodbye, World!");
https://tools.ietf.org/html/rfc6455#section-7.4
Attributes
● ReadyState
Attribute Name Attribute Value Description
WebSocket.CONNECTING 0 The connection is not yet open
WebSocket.OPEN 1 The connection is open and
ready to communicate
WebSocket.CLOSING 2 The connection is in the process of
closing
WebSocket.CLOSED 3 The connection is closed or couldn’t
be opened
● BufferedAmount
Ensuring all data is sent before closing a connection,
● Protocol
The handshake when completed should contain a selection from one that was sent by the client
Row 1 Row 2 Row 3 Row 4
0
2
4
6
8
10
12
Column 1
Column 2
Column 3
JSR-356
● Endpoint Classes
● javax.websocket.server.ServerEndpoint
● javax.websocket.ClientEndpoint
● Endpoint method-level annotations
● @OnOpen
● @OnClose
● @OnError
● @OnMessage
● http://www.oracle.com/technetwork/articles/java/jsr356-1937161.htm
XMLHttpRequest object properties
Property Description
• readyState An integer from 0. . .4. (0 means the call
is uninitialized, 4 means that the call is
complete.)
• onreadystatechange Determines the function called when the
objects readyState changes.
• responseText Data returned from the server as a text
string (read-only).
• responseXML Data returned from the server as an XML
document object (read-only).
• status HTTP status code returned by the server
• statusText HTTP status phrase returned by the server
We use the readyState to determine when the request has been completed, and then check the
status to see if it executed without an error. (We’ll see how to do this shortly.)
● readyState Holds the status of the
XMLHttpRequest. Changes from 0 to 4:
● 0: request not initialized
● 1: server connection established
● 2: request received
● 3: processing request
● 4: request finished and response is ready
Polling Techniques, Ajax, protocol Switching from Http to Websocket standard (RFC-6455), javascript engine for java (Nashorn) were discussed in presentation.
XMLHttpRequest object methods
Method Description
• open('method', 'URL', asyn) Specifies the HTTP method to be used (GET
or POST as a string, the target URL, and
whether or not the request should be
handled asynchronously (asyn should be
true or false, if omitted, true is
assumed).
• send(content) Sends the data for a POST request and
starts the request, if GET is used you
should call send(null).
• setRequestHeader('x','y') Sets a parameter and value pair x=y and
assigns it to the header to be sent with
the request.
• getAllResponseHeaders() Returns all headers as a string.
• getResponseHeader(x) Returns header x as a string.
• abort() Stops the current operation.
The open object method is used to set up the request, and the send method starts the request by
sending it to the server (with data for the server if the POST method is used).
Intercepting form submission & Ajax
● var xhttp;
● if (window.XMLHttpRequest) {
● xhttp = new XMLHttpRequest();
● } else {
● // code for IE6, IE5
● xhttp = new ActiveXObject("Microsoft.XMLHTTP");
● }
● xhttp.onreadystatechange = function() {
● if (xhttp.readyState == 4 && xhttp.status == 200) {
● document.getElementById("demo").innerHTML = xhttp.responseText;
● }
● }
● xhttp.open("GET/POST", "url", true/false);
● xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); -POST
● xhttp.send();
● http://jsfiddle.net/vintharas/9f7sb409/
● http://jsfiddle.net/clickthelink/Uwcuz/1/
Nashorn
● Java-to-JavaScript interoperability
● JavaScript Engine for the JVM
● Implementation of the ECMAScript Edition 5.1 Language
Specification
● Based on features of Da Vinci Machine - reference
implementation of (JSR) 292
● Pass the script to the 'jjs' or 'jrunscript' tool.
● javax.script package.
● ScriptEngineManager object.
ScriptEngineManager factory = new ScriptEngineManager();
// create a Nashorn script engine
ScriptEngine engine =
factory.getEngineByName("nashorn");
// evaluate JavaScript statement
try {
engine.eval("print('Hello, World!');");
} catch (final ScriptException se) { se.printStackTrace(); }
Accessing Java Classes
● Two approaches
– recommended - Java global object
– Traditional – Packages global object
Packages object
● Enables you to access Java packages and
classes using their fully qualified names, if they
are properties of the Packages object
● jjs> Packages.MyPackage
[JavaPackage MyPackage]
● jjs> Packages.MyPackage.MyClass
[JavaClass MyPackage.MyClass]
Accessing standard Java packages
●
jjs> java.lang
[JavaPackage java.lang]
●
jjs> typeof java.lang
object
●
jjs> java.lang.System
[JavaClass java.lang.System]
●
jjs> typeof java.lang.System
Function
https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nas
horn/api.html
Thank you
Srikanth Pallerla
+91-99-59-787-202
psrr.sri@gmail.com
srikanth.pallerla@vekomy.com
Ad

Recommended

M|18 Architectural Overview: MariaDB MaxScale
M|18 Architectural Overview: MariaDB MaxScale
MariaDB plc
 
MariaDB MaxScale: an Intelligent Database Proxy
MariaDB MaxScale: an Intelligent Database Proxy
Markus Mäkelä
 
Server system architecture
Server system architecture
Faiza Hafeez
 
M|18 Why Abstract Away the Underlying Database Infrastructure
M|18 Why Abstract Away the Underlying Database Infrastructure
MariaDB plc
 
MariaDB MaxScale: an Intelligent Database Proxy
MariaDB MaxScale: an Intelligent Database Proxy
Markus Mäkelä
 
Node.js 1, 2, 3
Node.js 1, 2, 3
Jian-Hong Pan
 
Find the bottleneck of your system
Find the bottleneck of your system
Jian-Hong Pan
 
Sharding in MongoDB 4.2 #what_is_new
Sharding in MongoDB 4.2 #what_is_new
Antonios Giannopoulos
 
Upgrading to MongoDB 4.0 from older versions
Upgrading to MongoDB 4.0 from older versions
Antonios Giannopoulos
 
Managing data and operation distribution in MongoDB
Managing data and operation distribution in MongoDB
Antonios Giannopoulos
 
MySQL Proxy
MySQL Proxy
Manikanda kumar
 
UltraESB - Advanced services
UltraESB - Advanced services
AdroitLogic
 
Using MongoDB with Kafka - Use Cases and Best Practices
Using MongoDB with Kafka - Use Cases and Best Practices
Antonios Giannopoulos
 
How to upgrade to MongoDB 4.0 - Percona Europe 2018
How to upgrade to MongoDB 4.0 - Percona Europe 2018
Antonios Giannopoulos
 
Datastage Online Training
Datastage Online Training
onlinetraining24
 
Triggers in MongoDB
Triggers in MongoDB
Antonios Giannopoulos
 
Web hdfs and httpfs
Web hdfs and httpfs
wchevreuil
 
Introduction to AdroitLogic and UltraESB
Introduction to AdroitLogic and UltraESB
AdroitLogic
 
Context-Oriented Programming: Beyond Layers
Context-Oriented Programming: Beyond Layers
ESUG
 
Shipping your logs to elk from mule app/cloudhub part 2
Shipping your logs to elk from mule app/cloudhub part 2
Alex Fernandez
 
Wait Events 10g
Wait Events 10g
sagai
 
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2
Antonios Giannopoulos
 
Sharded cluster tutorial
Sharded cluster tutorial
Antonios Giannopoulos
 
UltraESB - an introduction
UltraESB - an introduction
AdroitLogic
 
Locking And Concurrency
Locking And Concurrency
sqlserver.co.il
 
Building Next Generation Real-Time Web Applications using Websockets
Building Next Generation Real-Time Web Applications using Websockets
Naresh Chintalcheru
 
Unit-5.pptx
Unit-5.pptx
itzkuu01
 
HTML5/JavaScript Communication APIs - DPC 2014
HTML5/JavaScript Communication APIs - DPC 2014
Christian Wenz
 
Web Service and Mobile Integrated Day I
Web Service and Mobile Integrated Day I
Anuchit Chalothorn
 
Lecture 6 Web Sockets
Lecture 6 Web Sockets
Fahad Golra
 

More Related Content

What's hot (17)

Upgrading to MongoDB 4.0 from older versions
Upgrading to MongoDB 4.0 from older versions
Antonios Giannopoulos
 
Managing data and operation distribution in MongoDB
Managing data and operation distribution in MongoDB
Antonios Giannopoulos
 
MySQL Proxy
MySQL Proxy
Manikanda kumar
 
UltraESB - Advanced services
UltraESB - Advanced services
AdroitLogic
 
Using MongoDB with Kafka - Use Cases and Best Practices
Using MongoDB with Kafka - Use Cases and Best Practices
Antonios Giannopoulos
 
How to upgrade to MongoDB 4.0 - Percona Europe 2018
How to upgrade to MongoDB 4.0 - Percona Europe 2018
Antonios Giannopoulos
 
Datastage Online Training
Datastage Online Training
onlinetraining24
 
Triggers in MongoDB
Triggers in MongoDB
Antonios Giannopoulos
 
Web hdfs and httpfs
Web hdfs and httpfs
wchevreuil
 
Introduction to AdroitLogic and UltraESB
Introduction to AdroitLogic and UltraESB
AdroitLogic
 
Context-Oriented Programming: Beyond Layers
Context-Oriented Programming: Beyond Layers
ESUG
 
Shipping your logs to elk from mule app/cloudhub part 2
Shipping your logs to elk from mule app/cloudhub part 2
Alex Fernandez
 
Wait Events 10g
Wait Events 10g
sagai
 
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2
Antonios Giannopoulos
 
Sharded cluster tutorial
Sharded cluster tutorial
Antonios Giannopoulos
 
UltraESB - an introduction
UltraESB - an introduction
AdroitLogic
 
Locking And Concurrency
Locking And Concurrency
sqlserver.co.il
 
Upgrading to MongoDB 4.0 from older versions
Upgrading to MongoDB 4.0 from older versions
Antonios Giannopoulos
 
Managing data and operation distribution in MongoDB
Managing data and operation distribution in MongoDB
Antonios Giannopoulos
 
UltraESB - Advanced services
UltraESB - Advanced services
AdroitLogic
 
Using MongoDB with Kafka - Use Cases and Best Practices
Using MongoDB with Kafka - Use Cases and Best Practices
Antonios Giannopoulos
 
How to upgrade to MongoDB 4.0 - Percona Europe 2018
How to upgrade to MongoDB 4.0 - Percona Europe 2018
Antonios Giannopoulos
 
Web hdfs and httpfs
Web hdfs and httpfs
wchevreuil
 
Introduction to AdroitLogic and UltraESB
Introduction to AdroitLogic and UltraESB
AdroitLogic
 
Context-Oriented Programming: Beyond Layers
Context-Oriented Programming: Beyond Layers
ESUG
 
Shipping your logs to elk from mule app/cloudhub part 2
Shipping your logs to elk from mule app/cloudhub part 2
Alex Fernandez
 
Wait Events 10g
Wait Events 10g
sagai
 
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2
Antonios Giannopoulos
 
UltraESB - an introduction
UltraESB - an introduction
AdroitLogic
 

Similar to Polling Techniques, Ajax, protocol Switching from Http to Websocket standard (RFC-6455), javascript engine for java (Nashorn) were discussed in presentation. (20)

Building Next Generation Real-Time Web Applications using Websockets
Building Next Generation Real-Time Web Applications using Websockets
Naresh Chintalcheru
 
Unit-5.pptx
Unit-5.pptx
itzkuu01
 
HTML5/JavaScript Communication APIs - DPC 2014
HTML5/JavaScript Communication APIs - DPC 2014
Christian Wenz
 
Web Service and Mobile Integrated Day I
Web Service and Mobile Integrated Day I
Anuchit Chalothorn
 
Lecture 6 Web Sockets
Lecture 6 Web Sockets
Fahad Golra
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
WebStackAcademy
 
Go 1.8 'new' networking features
Go 1.8 'new' networking features
strikr .
 
Ajax tutorial by bally chohan
Ajax tutorial by bally chohan
WebVineet
 
Ajax
Ajax
rahmed_sct
 
BaseX user-group-talk XML Prague 2013
BaseX user-group-talk XML Prague 2013
Andy Bunce
 
KrakenD API Gateway
KrakenD API Gateway
Albert Lombarte
 
Spring 4 Web App
Spring 4 Web App
Rossen Stoyanchev
 
Socket programming, and openresty
Socket programming, and openresty
Tavish Naruka
 
Introduction to Ajax programming
Introduction to Ajax programming
Fulvio Corno
 
AJAX.pptx
AJAX.pptx
ssuser0a07a1
 
ITI006En-AJAX
ITI006En-AJAX
Huibert Aalbers
 
AJAX.pptx
AJAX.pptx
Ganesh Chavan
 
Web-Engineering-Lec-14 (1 ).pptx
Web-Engineering-Lec-14 (1 ).pptx
iamayesha2526
 
Web-Engineering-Lec-14 (1) .pptx
Web-Engineering-Lec-14 (1) .pptx
iamayesha2526
 
Ajax
Ajax
Bharath Palaksha
 
Building Next Generation Real-Time Web Applications using Websockets
Building Next Generation Real-Time Web Applications using Websockets
Naresh Chintalcheru
 
Unit-5.pptx
Unit-5.pptx
itzkuu01
 
HTML5/JavaScript Communication APIs - DPC 2014
HTML5/JavaScript Communication APIs - DPC 2014
Christian Wenz
 
Web Service and Mobile Integrated Day I
Web Service and Mobile Integrated Day I
Anuchit Chalothorn
 
Lecture 6 Web Sockets
Lecture 6 Web Sockets
Fahad Golra
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
WebStackAcademy
 
Go 1.8 'new' networking features
Go 1.8 'new' networking features
strikr .
 
Ajax tutorial by bally chohan
Ajax tutorial by bally chohan
WebVineet
 
BaseX user-group-talk XML Prague 2013
BaseX user-group-talk XML Prague 2013
Andy Bunce
 
Socket programming, and openresty
Socket programming, and openresty
Tavish Naruka
 
Introduction to Ajax programming
Introduction to Ajax programming
Fulvio Corno
 
Web-Engineering-Lec-14 (1 ).pptx
Web-Engineering-Lec-14 (1 ).pptx
iamayesha2526
 
Web-Engineering-Lec-14 (1) .pptx
Web-Engineering-Lec-14 (1) .pptx
iamayesha2526
 
Ad

Polling Techniques, Ajax, protocol Switching from Http to Websocket standard (RFC-6455), javascript engine for java (Nashorn) were discussed in presentation.

  • 1. Web Socket Client/Server communications Polling Vs LongPolling Vs Streaming HTTP half-duplex HTTP Overhead-less efficient communication
  • 3. To create desktop-like applications in the browser ● Developers used hacks in servers to keep connections open longer ● Open up resource allocation issues on servers ● Perceived latency to the end-user may be low, but ● Polling – unnecessary requests and stream of opening and closing connections ● No facility for layering other protocols on top of Comet or AJAX ● WebSocket gives you a bi-directional full-duplex communications channel that operates over HTTP through a single socket.
  • 4. WebSockets give you the... ● Ability to utilize an upgraded HTTP request ● Sends data in a message-based way similar to UDP ● Has the reliability of TCP. ● Single-connection, negligible penalty in resource uti‐lization, layer another protocol on top of WebSocket, a secure way over TLS
  • 5. WebSocket is an... ● Event-driven, full duplex, asynchronous communications channel with single TCP Connection ● Primary benefit is reducing resource needs on both the client and more importantly, the server. ● IETF standardized as RFC-6455 ● Utilizes HTTP as the initial transport mechanism ● Doesn’t end after a response is received by the client ● The client and server can freely send messages asynchronously without polling for anything new. ● Initialize the browser native WebSocket object var ws = new WebSocket("ws://localhost:8181", protocol (optional)); ● Sub-protocols so a single server can implement multiple WebSocket sub-protocols. (Registerd, open(XMPP, STOMP), custom)
  • 6. Events ● Open ws.onopen = function(e) { console.log("Connection established"); ws.send(JSON.stringify(stock_request)); }; ● Message ws.onmessage = function(e) { var _sData = JSON.parse(e.data); //can close connection }; ● Error ws.onerror = function(e) { console.log("WebSocket failure, error", e); handleErrors(e); }; ● PING / PONG – PING frames get sent out by the server only – Browser implementations should send back PONG frames in response ● Close ws.onclose = function(e) { console.log(e.reason + " " + e.code); };
  • 7. Methods ● Send var ws = new WebSocket("ws://localhost:8181"); ws.onopen = function(e) { ws.send(JSON.stringify(_request)); } ● Close ws.close(1000, "Goodbye, World!"); https://tools.ietf.org/html/rfc6455#section-7.4
  • 8. Attributes ● ReadyState Attribute Name Attribute Value Description WebSocket.CONNECTING 0 The connection is not yet open WebSocket.OPEN 1 The connection is open and ready to communicate WebSocket.CLOSING 2 The connection is in the process of closing WebSocket.CLOSED 3 The connection is closed or couldn’t be opened ● BufferedAmount Ensuring all data is sent before closing a connection, ● Protocol The handshake when completed should contain a selection from one that was sent by the client
  • 9. Row 1 Row 2 Row 3 Row 4 0 2 4 6 8 10 12 Column 1 Column 2 Column 3
  • 10. JSR-356 ● Endpoint Classes ● javax.websocket.server.ServerEndpoint ● javax.websocket.ClientEndpoint ● Endpoint method-level annotations ● @OnOpen ● @OnClose ● @OnError ● @OnMessage ● http://www.oracle.com/technetwork/articles/java/jsr356-1937161.htm
  • 11. XMLHttpRequest object properties Property Description • readyState An integer from 0. . .4. (0 means the call is uninitialized, 4 means that the call is complete.) • onreadystatechange Determines the function called when the objects readyState changes. • responseText Data returned from the server as a text string (read-only). • responseXML Data returned from the server as an XML document object (read-only). • status HTTP status code returned by the server • statusText HTTP status phrase returned by the server We use the readyState to determine when the request has been completed, and then check the status to see if it executed without an error. (We’ll see how to do this shortly.)
  • 12. ● readyState Holds the status of the XMLHttpRequest. Changes from 0 to 4: ● 0: request not initialized ● 1: server connection established ● 2: request received ● 3: processing request ● 4: request finished and response is ready
  • 14. XMLHttpRequest object methods Method Description • open('method', 'URL', asyn) Specifies the HTTP method to be used (GET or POST as a string, the target URL, and whether or not the request should be handled asynchronously (asyn should be true or false, if omitted, true is assumed). • send(content) Sends the data for a POST request and starts the request, if GET is used you should call send(null). • setRequestHeader('x','y') Sets a parameter and value pair x=y and assigns it to the header to be sent with the request. • getAllResponseHeaders() Returns all headers as a string. • getResponseHeader(x) Returns header x as a string. • abort() Stops the current operation. The open object method is used to set up the request, and the send method starts the request by sending it to the server (with data for the server if the POST method is used).
  • 15. Intercepting form submission & Ajax ● var xhttp; ● if (window.XMLHttpRequest) { ● xhttp = new XMLHttpRequest(); ● } else { ● // code for IE6, IE5 ● xhttp = new ActiveXObject("Microsoft.XMLHTTP"); ● } ● xhttp.onreadystatechange = function() { ● if (xhttp.readyState == 4 && xhttp.status == 200) { ● document.getElementById("demo").innerHTML = xhttp.responseText; ● } ● } ● xhttp.open("GET/POST", "url", true/false); ● xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); -POST ● xhttp.send(); ● http://jsfiddle.net/vintharas/9f7sb409/ ● http://jsfiddle.net/clickthelink/Uwcuz/1/
  • 16. Nashorn ● Java-to-JavaScript interoperability ● JavaScript Engine for the JVM ● Implementation of the ECMAScript Edition 5.1 Language Specification ● Based on features of Da Vinci Machine - reference implementation of (JSR) 292 ● Pass the script to the 'jjs' or 'jrunscript' tool. ● javax.script package. ● ScriptEngineManager object.
  • 17. ScriptEngineManager factory = new ScriptEngineManager(); // create a Nashorn script engine ScriptEngine engine = factory.getEngineByName("nashorn"); // evaluate JavaScript statement try { engine.eval("print('Hello, World!');"); } catch (final ScriptException se) { se.printStackTrace(); }
  • 18. Accessing Java Classes ● Two approaches – recommended - Java global object – Traditional – Packages global object
  • 19. Packages object ● Enables you to access Java packages and classes using their fully qualified names, if they are properties of the Packages object ● jjs> Packages.MyPackage [JavaPackage MyPackage] ● jjs> Packages.MyPackage.MyClass [JavaClass MyPackage.MyClass]
  • 20. Accessing standard Java packages ● jjs> java.lang [JavaPackage java.lang] ● jjs> typeof java.lang object ● jjs> java.lang.System [JavaClass java.lang.System] ● jjs> typeof java.lang.System Function https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nas horn/api.html