cd ~
wget https://raw.githubusercontent.com/robtweed/ewd-3-installers/master/ewd-xpress/createTestApp.sh
source createTestApp.sh test-app registered
*** got the ewd-register event!! sent: {"type":"test","params":{"text":"Hey! You clicked the button!"}}
raw received: {"type":"test","finished":true,"message":{"text":"You sent: Hey! You clicked the button!"}}
received: {"type":"test","finished":true,"message":{"text":"You sent: Hey! You clicked the button!"}}
button response: {"type":"test","finished":true,"message":{"text":"You sent: Hey! You clicked the button!"}} <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> $(document).ready(function() {
// etc...
}); EWD.start('test-app', null, io);
$(document).ready(function() {
EWD.on('ewd-registered', function() {
// Your application's logic can now be started here
});
EWD.start('test-app', null, io);
// or
//EWD.start('test-app', $);
});
<button onClick='sendMessage()'>Click me</button>and the sendMessage() function that it triggers sends an EWD 3 ewd-xpress message to the back-end. The structure of the message is the same as for EWD.js:
function sendMessage() {
var message = {
type: 'test',
params: {
text: 'Hey! You clicked the button!'
}
};
but the way you send it is by using the EWD.send() function:
EWD.send(message);There are two ways of handling the response, but we'll come back to that. Let's first look at what happens at the back-end when the message is received by socket.io.
ewd-xpress uses the ewd-qoper8-express module's Express middleware router which, in turn, adds the message to the ewd-qoper8 queue. It is immediately dispatched to the first available worker process (one is started up if necessary and the poolSize isn't exceeded). It then loads your application's back-end module and invokes your handler method for the incoming message type. This is almost identical to EWD.js.
So let's take a look at our test application's back-end module (see ~/ewd3/node_modules/test-app.js):
module.exports = {
handlers: {
test: function(messageObj, session, send, finished) {
var incomingText = messageObj.params.text;
finished({text: 'You sent: ' + incomingText});
}
}
};Again this should be familiar to those who have used EWD.js, but there are differences. The handlers are within the exported "handlers" object, and each handler function has a different set of arguments:
1) The incoming message object - this is the same as you sent from the browser
2) The back-end Session object for the user who sent this object. The Session is a DocumentNode object (see http://gradvs1.mgateway.com/download/ewd-document-store.pdf for the available APIs)
3) send - Function for returning any intermediate messages back to the browser - the send function prevents the worker process from being returned to the available pool. It should not be used for Ajax messaging!
4) finished = Function for returning the final message to the browser. The worker process is then returned to the available pool
In our case we're just echoing back the received message object as a new message object: {text: 'You sent: ' + incomingText}
Of course you also have access to your Cache or GT.M back-end. In EWD 3, it's made available via 'this.documentStore', eg:
var glo = new this.documentStore.DocumentNode('robTest', ['a']);
glo.value = 'Hello world';So that's the back-end. The response message is returned to the browser where you can handle it in one of two ways:
- using the EWD.send() function's callback function
- using Pub/Sub via the EWD.on('test') handler
The example application shows the use of the EWD.send() function's callback:
EWD.send(message, function(responseObj) {
console.log('button response: ' + JSON.stringify(responseObj));
});The returned message object contains the original type (responseObj.type) and the message payload (responseObj.message).
Alternatively you can add a Pub/Sub handler for this specific message type:
EWD.on('test', function(responseObj) {
// process the response object
});
The structure of the response object is identical to that consumed by the EWD.send() function's callback.
What you do with the response object in the browser is up to you.
And that's basically it for simple applications. You should have enough information to begin developing your own applications.
Just one convention - if you want to signal an error from your back-end handler functions, do this:
fihished({error: 'My error text'});
The response object structure is as before, eg:
{"type":"test","finished":true,"message":{"error":"You sent: Hey! You clicked the button!"}}but you'll find that some of the higher-level ewd-xpress modules look for this error message format and will automatically handle them
One other trick - for verbose logging in the JavaScript console, set:
EWD.log = true;and finally, whenever you modify a back-end ewd-xpress module, either restart ewd-xpress, or, better still, use the ewd-xpress-monitor application and close any running worker processes. New ones will be automatically spun up as needed by ewd-qoper8.
Enjoy developing with EWD 3!
Rob