Wrapper for EventSource HTML5 object to manage SSE (Server Sent Event) with ExtJS and Sencha Touch.
What is HTML5 EventSource object? A new way to do push notifications from the server to the client. Here's the specs about EventSource:
And now you can use it with ease with ExtJS and Sencha Touch: discover Ext.ux.EventSource wrapper!
First of all, install Bower.
Then install Ext.ux.EventSource
:
$ bower install ext.ux.eventsource
Now, you got the extension at the following path: YOUR_PROJECT_PATH/bower_components/ext.ux.eventsource/
It contains EventSource.js and a minified version EventSource.min.js.
Let's setup the Ext.Loader to require the right file:
Ext.Loader.setConfig ({
enabled: true ,
paths: {
'Ext.ux.EventSource': 'bower_components/ext.ux.eventsource/EventSource.js'
// or the minified one: 'Ext.ux.EventSource': 'bower_components/ext.ux.eventsource/EventSource.min.js'
}
});
Ext.require (['Ext.ux.EventSource']);
Load Ext.ux.EventSource
via Ext.require
:
Ext.Loader.setConfig ({
enabled: true
});
Ext.require (['Ext.ux.EventSource']);
Now, you are ready to use them in your code as follows:
// Creating a new instance of Ext.ux.EventSource
var eventsource = Ext.create ('Ext.ux.EventSource', {
url: 'your_url:your_port'
});
// Or
var eventsource = Ext.create ('Ext.ux.EventSource', 'your_url:your_port');
The communication is text-only, without objects or any other kind of data.
var eventsource = Ext.create ('Ext.ux.EventSource', {
url: 'http://localhost:3000/text' ,
listeners: {
open: function (es) {
console.log ('The eventsource is ready to use');
} ,
close: function (es) {
console.log ('The eventsource is closed!');
} ,
error: function (es, error) {
Ext.Error.raise (error);
} ,
message: function (es, message) {
console.log ('A new message is arrived: ' + message);
}
}
});
The communication is event-driven: an event and a String or Object are sent and the eventsource object handles different events.
var eventsource = Ext.create ('Ext.ux.EventSource', {
url: 'http://localhost:3000/event' ,
listeners: {
open: function (es) {
console.log ('The eventsource is ready to use');
} ,
close: function (es) {
console.log ('The eventsource is closed!');
}
}
});
// A 'stop' event is sent from the server
// 'data' has 'cmd' and 'msg' fields
eventsource.on ('stop', function (es, data) {
console.log ('Command: ' + data.cmd);
console.log ('Message: ' + data.msg);
});
The communication is mixed: it can handles text-only and event-driven communication.
var eventsource = Ext.create ('Ext.ux.EventSource', {
url: 'http://localhost:3000/both' ,
listeners: {
open: function (es) {
console.log ('The eventsource is ready to use');
} ,
close: function (es) {
console.log ('The eventsource is closed!');
} ,
message: function (es, message) {
console.log ('Text-only message arrived is: ' + message);
}
}
});
// A 'stop' event is sent from the server
// 'data' has 'cmd' and 'msg' fields
eventsource.on ('stop', function (data) {
console.log ('Command: ' + data.cmd);
console.log ('Message: ' + data.msg);
});
To start the demo, you need npm, so install it.
Then, launch the following command:
$ npm install
And then, start the demo:
$ grunt server
A page will appear on your browser at http://localhost:9000/demo/index.html
You can build the documentation (like ExtJS Docs) with jsduck:
$ jsduck ux --output /var/www/docs
It will make the documentation into docs dir and it will be visible at: http://localhost/docs
The MIT License (MIT)
Copyright (c) 2013 Vincenzo Ferrari wilk3ert@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.