SlideShare a Scribd company logo
Copyright ยฉ 2016 M/Gateway Developments Ltd
EWD 3 Training Course
Part 14
Using Ajax for QEWD messages
Rob Tweed
Director, M/Gateway Developments Ltd
Twitter: @rtweed
Copyright ยฉ 2016 M/Gateway Developments Ltd
Using Ajax for messages
โ€ข QEWD supports both WebSockets and
Ajax for message transport
โ€ข Ajax means message:
โ€“ Requests sent over HTTP
โ€“ Responses received as HTTP responses
โ€ข Why Ajax?
โ€“ Perhaps better scalability at very high end
โ€“ Well-understood protocol: some network
managers may be happier with it
Copyright ยฉ 2016 M/Gateway Developments Ltd
Using Ajax for messages
โ€ข Ajax / HTTP
โ€“ Messages can only be instigated by the client
โ€“ Back-end can't send messages to client except as a
response to a request
โ€“ A request must always return a response
โ€ข WebSockets
โ€“ Back-end can send messages to a client at any time,
without any initiating request
โ€ข No more polling
โ€“ "real-time" behaviour
โ€“ Growing evidence of it being a faster protocol
Copyright ยฉ 2016 M/Gateway Developments Ltd
How to use Ajax with QEWD
โ€ข Globally:
โ€“ Don't load socket.io into browser
โ€“ And/or remove io argument from EWD.start()
โ€ข Message-specific:
โ€“ Additional property for message object:
โ€ข ajax: true
โ€ข No change needed to back-end logic
โ€ข No need to restart back-end or workers
Copyright ยฉ 2016 M/Gateway Developments Ltd
Edit index.html
<html>
<head>
<title>Demo ewd-xpress application</title>
</head>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script src="/ewd-client.js"></script>
<script src="app.js"></script>
<button id="testBtn">Click Me</button>
<div id="content">
Content goes here
</div>
</body>
</html>
Copyright ยฉ 2016 M/Gateway Developments Ltd
Edit index.html
<html>
<head>
<title>Demo ewd-xpress application</title>
</head>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="/ewd-client.js"></script>
<script src="app.js"></script>
<button id="testBtn">Click Me</button>
<div id="content">
Content goes here
</div>
</body>
</html>
Copyright ยฉ 2016 M/Gateway Developments Ltd
Edit app.js
$(document).ready(function() {
EWD.log = true;
EWD.on('ewd-registered', function() {
EWD.on('intermediate', function(responseObj) {
$('#content').text(responseObj.message.date);
});
EWD.on('socketDisconnected', function() {
$('#content').text('You have been logged out');
$('#testBtn').hide();
});
$('#testBtn').on('click', function(e) {
var message = {type: 'testButton'};
EWD.send(message, function(messageObj) {
$('#content').append('<br /> ' + messageObj.message.ok);
});
});
});
EWD.start('demo1', $);
});
Remove 3rd argment (io)
Copyright ยฉ 2016 M/Gateway Developments Ltd
Try it out
Copyright ยฉ 2016 M/Gateway Developments Ltd
Intermediate messages over Ajax
โ€ข HTTP mandates 1 response only per
request
โ€ข Back-end handler must use finished() for
the single response
โ€“ To ensure that the worker is released
โ€ข send() function is disabled and ignored
โ€ข Hence no intermediate messages in
browser console log
Copyright ยฉ 2016 M/Gateway Developments Ltd
Message-specific Ajax
โ€ข Can optionally specify particular messages
to use Ajax, even if WebSockets enabled
Copyright ยฉ 2016 M/Gateway Developments Ltd
Edit app.js
$(document).ready(function() {
EWD.log = true;
EWD.on('ewd-registered', function() {
EWD.on('intermediate', function(responseObj) {
$('#content').text(responseObj.message.date);
});
EWD.on('socketDisconnected', function() {
$('#content').text('You have been logged out');
$('#testBtn').hide();
});
$('#testBtn').on('click', function(e) {
var message = {type: 'testButton'};
EWD.send(message, function(messageObj) {
$('#content').append('<br /> ' + messageObj.message.ok);
});
EWD.send({
type: 'ajaxTestMsg',
ajax: true
}, function(responseObj) {
console.log('response via Ajax: ' + JSON.stringify(responseObj));
});
});
});
EWD.start('demo1', $);
});
Copyright ยฉ 2016 M/Gateway Developments Ltd
Try it out
Copyright ยฉ 2016 M/Gateway Developments Ltd
Add back-end handler
โ€ข C:ewd3node_modulesdemo1.js
module.exports = {
handlers: {
testButton: function(messageObj, session, send, finished) {
session.data.$('foo').value = 'bar';
send({
type: 'intermediate',
foo: 'bar',
date: new Date().toString()
});
finished({
ok: 'testButton message was processed successfully!'
});
},
ajaxTestMsg: function(messageObj, session, send, finished) {
console.log('ajax message processed');
finished({
ok: 'ajax message processed successfully'
});
}
}
};
Standard handler pattern
But can't use send()
Copyright ยฉ 2016 M/Gateway Developments Ltd
Try it out
Copyright ยฉ 2016 M/Gateway Developments Ltd
Ajax Dependencies
โ€ข The built-in behaviour described here
relies on jQuery being loaded into the
browser
โ€ข jQuery isn't essential when using QEWD
โ€“ It's a default, but not mandatory, dependency
โ€ข If your preferred framework provides its
own Ajax interface function, this can be
integrated instead
Copyright ยฉ 2016 M/Gateway Developments Ltd
EWD.start()
EWD.start(appName, $, io, customAjaxFn,url)
appName = name of application for registration
$ = jQuery object (if being used)
io = socket.io object (if using WebSockets)
customAjaxFn = alternative Ajax handler function (if you want to
use a different framework from jQuery)
url = for use with non-browser clients, eg when using
React Native
Copyright ยฉ 2016 M/Gateway Developments Ltd
EWD.start()
EWD.start({
application: appName,
$: $,
io: io,
ajax: function(params, success, failure) {โ€ฆ },
url: url
});
Cleaner to use an object argument instead of
specifying multiple arguments by position
If not relevant, don't define them in the object
Copyright ยฉ 2016 M/Gateway Developments Ltd
EWD.start() with custom Ajax
EWD.start({
application: 'extJSDemo',
ajax: function(params, success, failure) {
console.log('sending message using custom Ajax function for ExtJS');
// ewd-client's ajax wrapper function provides the information you'll need in a params object, so here we re-map them for extJS
Ext.Ajax.request({
url: params.url,
method: params.type.toUpperCase(),
timeout: params.timeout,
jsonData: params.data,
success: function(response, opts) {
// similarly we invoke ewd-client's success function after mapping the extJS data
var data = Ext.decode(response.responseText);
success(data);
},
failure: function(response, opts) {
// and here we invoke ewd-client's failure function, after re-formatting the extJS failure text
console.log('Ajax server-side failure with status code ' + response.status);
failure(response.responseText);
}
});
}
});

More Related Content

What's hot (20)

PDF
EWD 3 Training Course Part 5b: First Steps in Building a QEWD Application
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3
Rob Tweed
ย 
PPT
EWD 3 Training Course Part 43: Using JSON Web Tokens with QEWD REST Services
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 2: EWD 3 Overview
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 4: Installing & Configuring QEWD
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 11: Handling Errors in QEWD
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 5a: First Steps in Building a QEWD Application
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 7: Applying the QEWD Messaging Pattern
Rob Tweed
ย 
PPT
QEWD.js, JSON Web Tokens & MicroServices
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 27: The QEWD Session
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 30: Modularising QEWD Applications
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 28: Integrating Legacy Mumps Code with QEWD
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 1: How Node.js Integrates With Global Storage Data...
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 10: QEWD Sessions and User Authentication
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 8: Anatomy of the QEWD Messaging Cycle
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 19: The cache.node APIs
Rob Tweed
ย 
PDF
qewd-ripple: The Ripple OSI Middle Tier
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 29: Running QEWD as a Service
Rob Tweed
ย 
PPT
ewd-qoper8-vistarpc: Exposing VistA's RPCs as REST Services
Rob Tweed
ย 
PDF
Composable and streamable Play apps
Yevgeniy Brikman
ย 
EWD 3 Training Course Part 5b: First Steps in Building a QEWD Application
Rob Tweed
ย 
EWD 3 Training Course Part 39: Building a React.js application with QEWD, Part 3
Rob Tweed
ย 
EWD 3 Training Course Part 43: Using JSON Web Tokens with QEWD REST Services
Rob Tweed
ย 
EWD 3 Training Course Part 2: EWD 3 Overview
Rob Tweed
ย 
EWD 3 Training Course Part 4: Installing & Configuring QEWD
Rob Tweed
ย 
EWD 3 Training Course Part 11: Handling Errors in QEWD
Rob Tweed
ย 
EWD 3 Training Course Part 5a: First Steps in Building a QEWD Application
Rob Tweed
ย 
EWD 3 Training Course Part 7: Applying the QEWD Messaging Pattern
Rob Tweed
ย 
QEWD.js, JSON Web Tokens & MicroServices
Rob Tweed
ย 
EWD 3 Training Course Part 27: The QEWD Session
Rob Tweed
ย 
EWD 3 Training Course Part 30: Modularising QEWD Applications
Rob Tweed
ย 
EWD 3 Training Course Part 28: Integrating Legacy Mumps Code with QEWD
Rob Tweed
ย 
EWD 3 Training Course Part 1: How Node.js Integrates With Global Storage Data...
Rob Tweed
ย 
EWD 3 Training Course Part 10: QEWD Sessions and User Authentication
Rob Tweed
ย 
EWD 3 Training Course Part 8: Anatomy of the QEWD Messaging Cycle
Rob Tweed
ย 
EWD 3 Training Course Part 19: The cache.node APIs
Rob Tweed
ย 
qewd-ripple: The Ripple OSI Middle Tier
Rob Tweed
ย 
EWD 3 Training Course Part 29: Running QEWD as a Service
Rob Tweed
ย 
ewd-qoper8-vistarpc: Exposing VistA's RPCs as REST Services
Rob Tweed
ย 
Composable and streamable Play apps
Yevgeniy Brikman
ย 

Similar to EWD 3 Training Course Part 14: Using Ajax for QEWD Messages (20)

PDF
EWD 3 Training Course Part 9: Complex QEWD Messages and Responses
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 6: What Happens when a QEWD Application is Started
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 12: QEWD Session Timeout Control
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 34: QEWD Resilient Mode
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 15: Using a Framework other than jQuery with QEWD
Rob Tweed
ย 
KEY
Socket.io
Timothy Fitz
ย 
PDF
How to Build Real-time Chat App with Express, ReactJS, and Socket.IO?
Katy Slemon
ย 
PDF
Make WordPress realtime.
Josh Hillier
ย 
PDF
Comet with node.js and V8
amix3k
ย 
PDF
Getting Started with WebSocket and Server-Sent Events in Java
Arun Gupta
ย 
PDF
Getting Started with WebSocket and Server-Sent Events using Java by Arun Gupta
Codemotion
ย 
PDF
Getting started with Websocket and Server-sent Events using Java - Arun Gupta
jaxconf
ย 
PPT
EWD 3 Training Course Part 45: Using QEWD's Advanced MicroService Functionality
Rob Tweed
ย 
PPT
Web-Socket
Pankaj Kumar Sharma
ย 
ODP
Polling Techniques, Ajax, protocol Switching from Http to Websocket standard ...
Srikanth Reddy Pallerla
ย 
PDF
How to build a chat application with react js, nodejs, and socket.io
Katy Slemon
ย 
KEY
[TW] Node.js
Bogdan Gaza
ย 
PPTX
Programming WebSockets with Glassfish and Grizzly
C2B2 Consulting
ย 
PDF
WebSocket Push Fallback - Transcript.pdf
ShaiAlmog1
ย 
PDF
EWD 3 Training Course Part 35: QEWD Session Locking
Rob Tweed
ย 
EWD 3 Training Course Part 9: Complex QEWD Messages and Responses
Rob Tweed
ย 
EWD 3 Training Course Part 6: What Happens when a QEWD Application is Started
Rob Tweed
ย 
EWD 3 Training Course Part 12: QEWD Session Timeout Control
Rob Tweed
ย 
EWD 3 Training Course Part 34: QEWD Resilient Mode
Rob Tweed
ย 
EWD 3 Training Course Part 15: Using a Framework other than jQuery with QEWD
Rob Tweed
ย 
Socket.io
Timothy Fitz
ย 
How to Build Real-time Chat App with Express, ReactJS, and Socket.IO?
Katy Slemon
ย 
Make WordPress realtime.
Josh Hillier
ย 
Comet with node.js and V8
amix3k
ย 
Getting Started with WebSocket and Server-Sent Events in Java
Arun Gupta
ย 
Getting Started with WebSocket and Server-Sent Events using Java by Arun Gupta
Codemotion
ย 
Getting started with Websocket and Server-sent Events using Java - Arun Gupta
jaxconf
ย 
EWD 3 Training Course Part 45: Using QEWD's Advanced MicroService Functionality
Rob Tweed
ย 
Web-Socket
Pankaj Kumar Sharma
ย 
Polling Techniques, Ajax, protocol Switching from Http to Websocket standard ...
Srikanth Reddy Pallerla
ย 
How to build a chat application with react js, nodejs, and socket.io
Katy Slemon
ย 
[TW] Node.js
Bogdan Gaza
ย 
Programming WebSockets with Glassfish and Grizzly
C2B2 Consulting
ย 
WebSocket Push Fallback - Transcript.pdf
ShaiAlmog1
ย 
EWD 3 Training Course Part 35: QEWD Session Locking
Rob Tweed
ย 
Ad

More from Rob Tweed (13)

PDF
QEWD Update
Rob Tweed
ย 
PPT
Data Persistence as a Language Feature
Rob Tweed
ย 
PPT
LNUG: Having Your Node.js Cake and Eating It Too
Rob Tweed
ย 
PPT
EWD 3 Training Course Part 44: Creating MicroServices with QEWD.js
Rob Tweed
ย 
PPT
QEWD.js: Have your Node.js Cake and Eat It Too
Rob Tweed
ย 
PPT
EWD 3 Training Course Part 42: The QEWD Docker Appliance
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 37: Building a React.js application with ewd-xpres...
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 33: Configuring QEWD to use CORS
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 32: Configuring QEWD to use SSL/HTTPS
Rob Tweed
ย 
PPT
EWD 3 Training Course Part 31: Using QEWD for Web and REST Services
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 26: Event-driven Indexing
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 25: Document Database Capabilities
Rob Tweed
ย 
PDF
EWD 3 Training Course Part 24: Traversing a Document's Leaf Nodes
Rob Tweed
ย 
QEWD Update
Rob Tweed
ย 
Data Persistence as a Language Feature
Rob Tweed
ย 
LNUG: Having Your Node.js Cake and Eating It Too
Rob Tweed
ย 
EWD 3 Training Course Part 44: Creating MicroServices with QEWD.js
Rob Tweed
ย 
QEWD.js: Have your Node.js Cake and Eat It Too
Rob Tweed
ย 
EWD 3 Training Course Part 42: The QEWD Docker Appliance
Rob Tweed
ย 
EWD 3 Training Course Part 37: Building a React.js application with ewd-xpres...
Rob Tweed
ย 
EWD 3 Training Course Part 33: Configuring QEWD to use CORS
Rob Tweed
ย 
EWD 3 Training Course Part 32: Configuring QEWD to use SSL/HTTPS
Rob Tweed
ย 
EWD 3 Training Course Part 31: Using QEWD for Web and REST Services
Rob Tweed
ย 
EWD 3 Training Course Part 26: Event-driven Indexing
Rob Tweed
ย 
EWD 3 Training Course Part 25: Document Database Capabilities
Rob Tweed
ย 
EWD 3 Training Course Part 24: Traversing a Document's Leaf Nodes
Rob Tweed
ย 
Ad

Recently uploaded (20)

PDF
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
ย 
PPTX
For my supp to finally picking supp that work
necas19388
ย 
PDF
From Chaos to Clarity: Mastering Analytics Governance in the Modern Enterprise
Wiiisdom
ย 
PDF
Power BI vs Tableau vs Looker - Which BI Tool is Right for You?
MagnusMinds IT Solution LLP
ย 
PPTX
ManageIQ - Sprint 264 Review - Slide Deck
ManageIQ
ย 
PPTX
Perfecting XM Cloud for Multisite Setup.pptx
Ahmed Okour
ย 
PDF
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
ย 
PDF
Dealing with JSON in the relational world
Andres Almiray
ย 
PPTX
CONCEPT OF PROGRAMMING in language .pptx
tamim41
ย 
PDF
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
ย 
PPTX
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
ย 
PPTX
computer forensics encase emager app exp6 1.pptx
ssuser343e92
ย 
PDF
2025ๅนด Linux ๆ ธๅฟƒๅฐˆ้กŒ: ๆŽข่จŽ sched_ext ๅŠๆฉŸๅ™จๅญธ็ฟ’.pdf
Eric Chou
ย 
PPTX
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
ย 
PDF
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
ย 
PDF
Transparency into Your Softwareโ€™s True Reach
team-WIBU
ย 
PPTX
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
ย 
PDF
IObit Uninstaller Pro 14.3.1.8 Crack for Windows Latest
utfefguu
ย 
PDF
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
ย 
PPTX
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
ย 
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
ย 
For my supp to finally picking supp that work
necas19388
ย 
From Chaos to Clarity: Mastering Analytics Governance in the Modern Enterprise
Wiiisdom
ย 
Power BI vs Tableau vs Looker - Which BI Tool is Right for You?
MagnusMinds IT Solution LLP
ย 
ManageIQ - Sprint 264 Review - Slide Deck
ManageIQ
ย 
Perfecting XM Cloud for Multisite Setup.pptx
Ahmed Okour
ย 
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
ย 
Dealing with JSON in the relational world
Andres Almiray
ย 
CONCEPT OF PROGRAMMING in language .pptx
tamim41
ย 
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
ย 
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
ย 
computer forensics encase emager app exp6 1.pptx
ssuser343e92
ย 
2025ๅนด Linux ๆ ธๅฟƒๅฐˆ้กŒ: ๆŽข่จŽ sched_ext ๅŠๆฉŸๅ™จๅญธ็ฟ’.pdf
Eric Chou
ย 
IObit Driver Booster Pro Crack Download Latest Version
chaudhryakashoo065
ย 
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
ย 
Transparency into Your Softwareโ€™s True Reach
team-WIBU
ย 
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
ย 
IObit Uninstaller Pro 14.3.1.8 Crack for Windows Latest
utfefguu
ย 
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
ย 
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
ย 

EWD 3 Training Course Part 14: Using Ajax for QEWD Messages

  • 1. Copyright ยฉ 2016 M/Gateway Developments Ltd EWD 3 Training Course Part 14 Using Ajax for QEWD messages Rob Tweed Director, M/Gateway Developments Ltd Twitter: @rtweed
  • 2. Copyright ยฉ 2016 M/Gateway Developments Ltd Using Ajax for messages โ€ข QEWD supports both WebSockets and Ajax for message transport โ€ข Ajax means message: โ€“ Requests sent over HTTP โ€“ Responses received as HTTP responses โ€ข Why Ajax? โ€“ Perhaps better scalability at very high end โ€“ Well-understood protocol: some network managers may be happier with it
  • 3. Copyright ยฉ 2016 M/Gateway Developments Ltd Using Ajax for messages โ€ข Ajax / HTTP โ€“ Messages can only be instigated by the client โ€“ Back-end can't send messages to client except as a response to a request โ€“ A request must always return a response โ€ข WebSockets โ€“ Back-end can send messages to a client at any time, without any initiating request โ€ข No more polling โ€“ "real-time" behaviour โ€“ Growing evidence of it being a faster protocol
  • 4. Copyright ยฉ 2016 M/Gateway Developments Ltd How to use Ajax with QEWD โ€ข Globally: โ€“ Don't load socket.io into browser โ€“ And/or remove io argument from EWD.start() โ€ข Message-specific: โ€“ Additional property for message object: โ€ข ajax: true โ€ข No change needed to back-end logic โ€ข No need to restart back-end or workers
  • 5. Copyright ยฉ 2016 M/Gateway Developments Ltd Edit index.html <html> <head> <title>Demo ewd-xpress application</title> </head> <body> <script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> <script src="/socket.io/socket.io.js"></script> <script src="/ewd-client.js"></script> <script src="app.js"></script> <button id="testBtn">Click Me</button> <div id="content"> Content goes here </div> </body> </html>
  • 6. Copyright ยฉ 2016 M/Gateway Developments Ltd Edit index.html <html> <head> <title>Demo ewd-xpress application</title> </head> <body> <script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> <script src="/ewd-client.js"></script> <script src="app.js"></script> <button id="testBtn">Click Me</button> <div id="content"> Content goes here </div> </body> </html>
  • 7. Copyright ยฉ 2016 M/Gateway Developments Ltd Edit app.js $(document).ready(function() { EWD.log = true; EWD.on('ewd-registered', function() { EWD.on('intermediate', function(responseObj) { $('#content').text(responseObj.message.date); }); EWD.on('socketDisconnected', function() { $('#content').text('You have been logged out'); $('#testBtn').hide(); }); $('#testBtn').on('click', function(e) { var message = {type: 'testButton'}; EWD.send(message, function(messageObj) { $('#content').append('<br /> ' + messageObj.message.ok); }); }); }); EWD.start('demo1', $); }); Remove 3rd argment (io)
  • 8. Copyright ยฉ 2016 M/Gateway Developments Ltd Try it out
  • 9. Copyright ยฉ 2016 M/Gateway Developments Ltd Intermediate messages over Ajax โ€ข HTTP mandates 1 response only per request โ€ข Back-end handler must use finished() for the single response โ€“ To ensure that the worker is released โ€ข send() function is disabled and ignored โ€ข Hence no intermediate messages in browser console log
  • 10. Copyright ยฉ 2016 M/Gateway Developments Ltd Message-specific Ajax โ€ข Can optionally specify particular messages to use Ajax, even if WebSockets enabled
  • 11. Copyright ยฉ 2016 M/Gateway Developments Ltd Edit app.js $(document).ready(function() { EWD.log = true; EWD.on('ewd-registered', function() { EWD.on('intermediate', function(responseObj) { $('#content').text(responseObj.message.date); }); EWD.on('socketDisconnected', function() { $('#content').text('You have been logged out'); $('#testBtn').hide(); }); $('#testBtn').on('click', function(e) { var message = {type: 'testButton'}; EWD.send(message, function(messageObj) { $('#content').append('<br /> ' + messageObj.message.ok); }); EWD.send({ type: 'ajaxTestMsg', ajax: true }, function(responseObj) { console.log('response via Ajax: ' + JSON.stringify(responseObj)); }); }); }); EWD.start('demo1', $); });
  • 12. Copyright ยฉ 2016 M/Gateway Developments Ltd Try it out
  • 13. Copyright ยฉ 2016 M/Gateway Developments Ltd Add back-end handler โ€ข C:ewd3node_modulesdemo1.js module.exports = { handlers: { testButton: function(messageObj, session, send, finished) { session.data.$('foo').value = 'bar'; send({ type: 'intermediate', foo: 'bar', date: new Date().toString() }); finished({ ok: 'testButton message was processed successfully!' }); }, ajaxTestMsg: function(messageObj, session, send, finished) { console.log('ajax message processed'); finished({ ok: 'ajax message processed successfully' }); } } }; Standard handler pattern But can't use send()
  • 14. Copyright ยฉ 2016 M/Gateway Developments Ltd Try it out
  • 15. Copyright ยฉ 2016 M/Gateway Developments Ltd Ajax Dependencies โ€ข The built-in behaviour described here relies on jQuery being loaded into the browser โ€ข jQuery isn't essential when using QEWD โ€“ It's a default, but not mandatory, dependency โ€ข If your preferred framework provides its own Ajax interface function, this can be integrated instead
  • 16. Copyright ยฉ 2016 M/Gateway Developments Ltd EWD.start() EWD.start(appName, $, io, customAjaxFn,url) appName = name of application for registration $ = jQuery object (if being used) io = socket.io object (if using WebSockets) customAjaxFn = alternative Ajax handler function (if you want to use a different framework from jQuery) url = for use with non-browser clients, eg when using React Native
  • 17. Copyright ยฉ 2016 M/Gateway Developments Ltd EWD.start() EWD.start({ application: appName, $: $, io: io, ajax: function(params, success, failure) {โ€ฆ }, url: url }); Cleaner to use an object argument instead of specifying multiple arguments by position If not relevant, don't define them in the object
  • 18. Copyright ยฉ 2016 M/Gateway Developments Ltd EWD.start() with custom Ajax EWD.start({ application: 'extJSDemo', ajax: function(params, success, failure) { console.log('sending message using custom Ajax function for ExtJS'); // ewd-client's ajax wrapper function provides the information you'll need in a params object, so here we re-map them for extJS Ext.Ajax.request({ url: params.url, method: params.type.toUpperCase(), timeout: params.timeout, jsonData: params.data, success: function(response, opts) { // similarly we invoke ewd-client's success function after mapping the extJS data var data = Ext.decode(response.responseText); success(data); }, failure: function(response, opts) { // and here we invoke ewd-client's failure function, after re-formatting the extJS failure text console.log('Ajax server-side failure with status code ' + response.status); failure(response.responseText); } }); } });