SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Web Socket
Client/Server communications
Polling Vs LongPolling Vs Streaming
HTTP half-duplex
HTTP Overhead-less efficient communication
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
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

Weitere ähnliche Inhalte

Was ist angesagt?

Upgrading to MongoDB 4.0 from older versions
Upgrading to MongoDB 4.0 from older versionsUpgrading to MongoDB 4.0 from older versions
Upgrading to MongoDB 4.0 from older versionsAntonios Giannopoulos
 
Managing data and operation distribution in MongoDB
Managing data and operation distribution in MongoDBManaging data and operation distribution in MongoDB
Managing data and operation distribution in MongoDBAntonios Giannopoulos
 
UltraESB - Advanced services
UltraESB - Advanced servicesUltraESB - Advanced services
UltraESB - Advanced servicesAdroitLogic
 
Using MongoDB with Kafka - Use Cases and Best Practices
Using MongoDB with Kafka -  Use Cases and Best PracticesUsing MongoDB with Kafka -  Use Cases and Best Practices
Using MongoDB with Kafka - Use Cases and Best PracticesAntonios Giannopoulos
 
How to upgrade to MongoDB 4.0 - Percona Europe 2018
How to upgrade to MongoDB 4.0 - Percona Europe 2018How to upgrade to MongoDB 4.0 - Percona Europe 2018
How to upgrade to MongoDB 4.0 - Percona Europe 2018Antonios Giannopoulos
 
Web hdfs and httpfs
Web hdfs and httpfsWeb hdfs and httpfs
Web hdfs and httpfswchevreuil
 
Introduction to AdroitLogic and UltraESB
Introduction to AdroitLogic and UltraESBIntroduction to AdroitLogic and UltraESB
Introduction to AdroitLogic and UltraESBAdroitLogic
 
Context-Oriented Programming: Beyond Layers
Context-Oriented Programming: Beyond LayersContext-Oriented Programming: Beyond Layers
Context-Oriented Programming: Beyond LayersESUG
 
Shipping your logs to elk from mule app/cloudhub part 2
Shipping your logs to elk from mule app/cloudhub   part 2Shipping your logs to elk from mule app/cloudhub   part 2
Shipping your logs to elk from mule app/cloudhub part 2Alex Fernandez
 
Wait Events 10g
Wait Events 10gWait Events 10g
Wait Events 10gsagai
 
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2Antonios Giannopoulos
 
UltraESB - an introduction
UltraESB - an introductionUltraESB - an introduction
UltraESB - an introductionAdroitLogic
 

Was ist angesagt? (17)

Upgrading to MongoDB 4.0 from older versions
Upgrading to MongoDB 4.0 from older versionsUpgrading to MongoDB 4.0 from older versions
Upgrading to MongoDB 4.0 from older versions
 
Managing data and operation distribution in MongoDB
Managing data and operation distribution in MongoDBManaging data and operation distribution in MongoDB
Managing data and operation distribution in MongoDB
 
MySQL Proxy
MySQL ProxyMySQL Proxy
MySQL Proxy
 
UltraESB - Advanced services
UltraESB - Advanced servicesUltraESB - Advanced services
UltraESB - Advanced services
 
Using MongoDB with Kafka - Use Cases and Best Practices
Using MongoDB with Kafka -  Use Cases and Best PracticesUsing MongoDB with Kafka -  Use Cases and Best Practices
Using MongoDB with Kafka - Use Cases and Best Practices
 
How to upgrade to MongoDB 4.0 - Percona Europe 2018
How to upgrade to MongoDB 4.0 - Percona Europe 2018How to upgrade to MongoDB 4.0 - Percona Europe 2018
How to upgrade to MongoDB 4.0 - Percona Europe 2018
 
Datastage Online Training
Datastage Online TrainingDatastage Online Training
Datastage Online Training
 
Triggers in MongoDB
Triggers in MongoDBTriggers in MongoDB
Triggers in MongoDB
 
Web hdfs and httpfs
Web hdfs and httpfsWeb hdfs and httpfs
Web hdfs and httpfs
 
Introduction to AdroitLogic and UltraESB
Introduction to AdroitLogic and UltraESBIntroduction to AdroitLogic and UltraESB
Introduction to AdroitLogic and UltraESB
 
Context-Oriented Programming: Beyond Layers
Context-Oriented Programming: Beyond LayersContext-Oriented Programming: Beyond Layers
Context-Oriented Programming: Beyond Layers
 
Shipping your logs to elk from mule app/cloudhub part 2
Shipping your logs to elk from mule app/cloudhub   part 2Shipping your logs to elk from mule app/cloudhub   part 2
Shipping your logs to elk from mule app/cloudhub part 2
 
Wait Events 10g
Wait Events 10gWait Events 10g
Wait Events 10g
 
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2
New Indexing and Aggregation Pipeline Capabilities in MongoDB 4.2
 
Sharded cluster tutorial
Sharded cluster tutorialSharded cluster tutorial
Sharded cluster tutorial
 
UltraESB - an introduction
UltraESB - an introductionUltraESB - an introduction
UltraESB - an introduction
 
Locking And Concurrency
Locking And ConcurrencyLocking And Concurrency
Locking And Concurrency
 

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

Building Next Generation Real-Time Web Applications using Websockets
Building Next Generation Real-Time Web Applications using WebsocketsBuilding Next Generation Real-Time Web Applications using Websockets
Building Next Generation Real-Time Web Applications using WebsocketsNaresh Chintalcheru
 
Unit-5.pptx
Unit-5.pptxUnit-5.pptx
Unit-5.pptxitzkuu01
 
HTML5/JavaScript Communication APIs - DPC 2014
HTML5/JavaScript Communication APIs - DPC 2014HTML5/JavaScript Communication APIs - DPC 2014
HTML5/JavaScript Communication APIs - DPC 2014Christian Wenz
 
Web Service and Mobile Integrated Day I
Web Service and Mobile Integrated Day IWeb Service and Mobile Integrated Day I
Web Service and Mobile Integrated Day IAnuchit Chalothorn
 
Lecture 6 Web Sockets
Lecture 6   Web SocketsLecture 6   Web Sockets
Lecture 6 Web SocketsFahad Golra
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
 Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4... 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 featuresGo 1.8 'new' networking features
Go 1.8 'new' networking featuresstrikr .
 
Ajax tutorial by bally chohan
Ajax tutorial by bally chohanAjax tutorial by bally chohan
Ajax tutorial by bally chohanWebVineet
 
BaseX user-group-talk XML Prague 2013
BaseX user-group-talk XML Prague 2013BaseX user-group-talk XML Prague 2013
BaseX user-group-talk XML Prague 2013Andy Bunce
 
Socket programming, and openresty
Socket programming, and openrestySocket programming, and openresty
Socket programming, and openrestyTavish Naruka
 
Introduction to Ajax programming
Introduction to Ajax programmingIntroduction to Ajax programming
Introduction to Ajax programmingFulvio Corno
 
21servers And Applets
21servers And Applets21servers And Applets
21servers And AppletsAdil Jafri
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in JavaTushar B Kute
 

Ähnlich wie 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 WebsocketsBuilding Next Generation Real-Time Web Applications using Websockets
Building Next Generation Real-Time Web Applications using Websockets
 
Unit-5.pptx
Unit-5.pptxUnit-5.pptx
Unit-5.pptx
 
HTML5/JavaScript Communication APIs - DPC 2014
HTML5/JavaScript Communication APIs - DPC 2014HTML5/JavaScript Communication APIs - DPC 2014
HTML5/JavaScript Communication APIs - DPC 2014
 
Web Service and Mobile Integrated Day I
Web Service and Mobile Integrated Day IWeb Service and Mobile Integrated Day I
Web Service and Mobile Integrated Day I
 
Lecture 6 Web Sockets
Lecture 6   Web SocketsLecture 6   Web Sockets
Lecture 6 Web Sockets
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
 Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4... Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
 
Go 1.8 'new' networking features
Go 1.8 'new' networking featuresGo 1.8 'new' networking features
Go 1.8 'new' networking features
 
Ajax tutorial by bally chohan
Ajax tutorial by bally chohanAjax tutorial by bally chohan
Ajax tutorial by bally chohan
 
Ajax
AjaxAjax
Ajax
 
BaseX user-group-talk XML Prague 2013
BaseX user-group-talk XML Prague 2013BaseX user-group-talk XML Prague 2013
BaseX user-group-talk XML Prague 2013
 
KrakenD API Gateway
KrakenD API GatewayKrakenD API Gateway
KrakenD API Gateway
 
Spring 4 Web App
Spring 4 Web AppSpring 4 Web App
Spring 4 Web App
 
Socket programming, and openresty
Socket programming, and openrestySocket programming, and openresty
Socket programming, and openresty
 
Introduction to Ajax programming
Introduction to Ajax programmingIntroduction to Ajax programming
Introduction to Ajax programming
 
AJAX.pptx
AJAX.pptxAJAX.pptx
AJAX.pptx
 
ITI006En-AJAX
ITI006En-AJAXITI006En-AJAX
ITI006En-AJAX
 
AJAX.pptx
AJAX.pptxAJAX.pptx
AJAX.pptx
 
Ajax
AjaxAjax
Ajax
 
21servers And Applets
21servers And Applets21servers And Applets
21servers And Applets
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 

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
  • 2.
  • 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
  • 13.
  • 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