Driving Behavioral Change for Information Management through Data-Driven Gree...
Building Real-Time Applications with Android and WebSockets
1. Real Time applications with
WebSockets / WorkShop
Sergi Almar i graupera
@sergialmar
Romanian mobile systems community conference (mobos)
November 2013 - cluj Napoca
2. Agenda
•
Part 1 - Architecture and Dependency Injection with Android
•
Part 2 - Building the Server in Node.js and Socket.io
•
Part 3 - Building the Android client
7. WebSockets
•
Real-time full duplex communication over TCP
•
Uses port 80 / 443 (URL scheme: ws:// and wss://)
•
Small overhead for text messages (frames)
•
•
0x00 for frame start, 0xFF for frame end (vs HTTP 1K)
Ping / pong frames for staying alive
9. WebSocket API
var ws = new WebSocket('ws://www.romobos.com/ws');
!
// When the connection is open, send some data to the server
ws.onopen = function () {
ws.send('Ping'); // Send the message 'Ping' to the server
};
!
// Log errors
ws.onerror = function (error) {
ws.log('WebSocket Error ' + error);
};
!
// Log messages from the server
ws.onmessage = function (e) {
ws.log('Server: ' + e.data);
};
10. What we are gonna build
(real-time chat app)
websoc
kets
w
ckets
ebso
18. Custom Binding
public class MyModule implements Module {
@Override
public void configure(Binder binder) {
binder.bind(IFoo.class).to(SimpleFoo.class);
}
}
define a module
public class App extends Application {
!
!
@Override
public void onCreate() {
super.onCreate();
RoboGuice.setBaseApplicationInjector(this,
RoboGuice.DEFAULT_STAGE,
RoboGuice.newDefaultRoboModule(this),
new MyModule());
}
}
let RoboGuice know about it
24. Google V8 Engine
•
Open source JS engine by Google (used in Google
Chrome)
•
No JIT, all JS compiled to assembler
•
Optimisations like inlining, elision of runtime
properties…
•
Improved garbage collector
25. CommonJS
•
Set of specifications for JS outside the browser
•
Node.js implements some specifications
•
i.e modules
•
There should be a function called require
•
There should be a var called exports
26. Modules
•
Node.js provides some core modules like http, tcp, fs, sys…
•
will look for the module in the node_modules dir
hierarchically
•
if not found, will look in the paths outlined in
NODE_PATH
var http = require('http');
27. Module Example
var PI = Math.PI;
exports.area = function (r) {
return PI * r * r;
};
exports.circumference = function (r) {
return 2 * PI * r;
};
module definition in myModule.js
var myModule = require('./myModule.js');
include myModule.js in some other file
28. Dependency Management
Node packet manager (npm)
express (routing), socket.io (websockets)…
Sergis-MacBook-Air:tmp salmar$ npm install express
npm http GET https://registry.npmjs.org/express
npm http 200 https://registry.npmjs.org/express
npm http GET https://registry.npmjs.org/express/-/express-3.4.4.tgz
npm http 200 https://registry.npmjs.org/express/-/express-3.4.4.tgz
npm http GET https://registry.npmjs.org/connect/2.11.0
npm http GET https://registry.npmjs.org/commander/1.3.2
npm http GET https://registry.npmjs.org/methods/0.1.0
npm http GET https://registry.npmjs.org/range-parser/0.0.4
npm http GET https://registry.npmjs.org/mkdirp/0.3.5
npm http GET https://registry.npmjs.org/cookie/0.1.0
npm http GET https://registry.npmjs.org/buffer-crc32/0.2.1
npm http GET https://registry.npmjs.org/fresh/0.2.0
npm http GET https://registry.npmjs.org/cookie-signature/1.0.1
npm http GET https://registry.npmjs.org/send/0.1.4
npm http GET https://registry.npmjs.org/debug
npm http 200 https://registry.npmjs.org/methods/0.1.0
npm http 304 https://registry.npmjs.org/range-parser/0.0.4
npm http GET https://registry.npmjs.org/methods/-/methods-0.1.0.tgz
npm http 200 https://registry.npmjs.org/commander/1.3.2
npm http GET https://registry.npmjs.org/commander/-/commander-1.3.2.tgz
npm http 304 https://registry.npmjs.org/cookie/0.1.0
…
!
express@3.4.4 node_modules/express
├── methods@0.1.0
├── range-parser@0.0.4
├── cookie-signature@1.0.1
├── fresh@0.2.0
├── debug@0.7.4
├── buffer-crc32@0.2.1
├── cookie@0.1.0
├── mkdirp@0.3.5
├── commander@1.3.2 (keypress@0.1.0)
├── send@0.1.4 (mime@1.2.11)
└── connect@2.11.0 (methods@0.0.1, uid2@0.0.3, pause@0.0.1, qs@0.6.5, raw-body@0.0.3, bytes@0.2.1, negotiator@0.3.0,
39. Emitting Events
socket.emit('user:join', {name: 'sergi'});
triggers a custom event
socket.broadcast.emit('user:joined', data);
sends a message to all clients except the owner of the socket
40. Attach information to the socket
socket.set('nickname', data.name, <optional_callback>);
46. Publishing
Bus bus = new Bus();
creates the bus (better use dependency injection)
bus.post(new ServerMessage("This is awesome"));
publish the message
synchronous delivery
49. SocketIO for Android
•
There’s no official library, but there are community
libraries, sometimes buggy :(
•
•
https://github.com/fatshotty/socket.io-java-client
Server-like API
50. Callbacks
!
!
!
!
!
public void onMessage(JsonElement json, IOAcknowledge ack) {
}
public void onMessage(String data, IOAcknowledge ack) {
}
public void onError(SocketIOException socketIOException) {
}
public void onDisconnect() {
}
public void onConnect() {
}
public void on(String event, IOAcknowledge ack, JsonElement... args) {
}