Вы находитесь на странице: 1из 26

Watson IBM i WebSockets

Aaron Bartell
Director of IBM i Innovation

albartell@krengeltech.com
Copyright 2015 Aaron Bartell
This session brought to you by...

● Consulting - Jumpstart your open source pursuit. Small


and big projects.

● Free Educational Content on everything open source for


IBM i at litmis.com

● spaces.litmis.com provides open source development via


browser on IBM i machine in the cloud.

twitter: @litmisteam albartell@krengeltech.com


agenda

What are WebSockets?

Simple WebSocket chat app

What is Watson?

Watson Speech-to-text app

WRKACTJOB and WebSockets


what is a websocket?
WebSocket logo. I also
Full-duplex communication don't know what it means,
but it sure looks cool!
New protocols, ws:// and wss:// for standard and secure WebSocket connections.

Uses TCP (lower level than HTTP) for transport

WebSockets is actually NOT HTTP, it just starts out at HTTP and then changes (aka
"upgrades") the connection to WebSockets.

Uses socket descriptors to retain state. IBM i has max of 2.5 million descriptors per job
(wowza!)

Works over existing 80 and 443 ports to escape firewall issues.

Part of HTML5 spec

bit.ly/SO-websocket-connections
why?

Low latency

Server can initiate subsequent communication

Have you used Google Docs?

docs.google.com
before websockets...

… there was long polling

bit.ly/pubnub-longpolling
high level

photo creds to pubnum.com


the handshake (raw)
Request
GET ws://mydomain.com/?encoding=text HTTP/1.1
Origin: http://mydomain.com
Cookie: __utma=99as
Connection: Upgrade Success!
Host: mydomain.com
Sec-WebSocket-Key: uRovscZjNol/umbTt5uKmw==
Upgrade: websocket
Sec-WebSocket-Version: 13

Response
HTTP/1.1 101 Switching Protocols Success!
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: iXFYxLieDcAue5MC56SsA3qX8zE=
Sec-WebSocket-Extensions: permessage-deflate
websocket frames

Average size of http headers alone is


800 bytes. Now we are down to only
sending content(much smaller).

bit.ly/mozilla-websocket-server-frames
events

var connection = new WebSocket('ws://mydomain.com/some-endpoint')

connection.onopen = function(e) {
console.log("Connected");
};

connection.onmessage = function(e) {
console.log( "Received: " + e.data);
};

connection.onclose = function(e) {
console.log("Connection closed");
};

bit.ly/SO-websocket-connections
cross-domain

WebSockets is cross-domain by default

Up to you to optionally restrict domain access on server via Origin header

This space intentionally left blank

bit.ly/SO-websocket-connections
browser-based dev tools
cloud9

ungit

These are
running on IBM i!
ibm i chat
- Runs on IBM i
- Written in Node.js
- Uses HTML5's WebSockets to communicate
- socket.io library used for client and server
- Article: bit.ly/nodejs-is-genius-with-websockets
app.js

var app = require('express')();


var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){


res.sendfile('index.html');
});

io.on('connection', function(server){
server.on('disconnect', function(){
console.log('user disconnected');
});
server.on('chat_msg', function(msg){
io.emit('chat_msg', msg);
});
});

http.listen(8001, function(){
console.log('listening on *:8001');
});
index.html
<html>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
var client = io();

$('form').submit(function(){
client.emit('chat_msg', $('#m').val());
$('#m').val('');
return false;
});

client.on('chat_msg', function(msg){
$('#messages').append($('<li>').text(msg));
});

</script>
</body>
</html>
Cognitive computing is the simulation of human thought processes in
a computerized model. Cognitive computing involves self-learning Tone
systems that use data mining, pattern recognition and natural Analyzer
language processing to mimic the way the human brain works.

Over 19 services with new


ones regularly coming

Document
Conversion

Alchemy
Data News

Speech Visual
To Text Recognition

bit.ly/watson-services-catalog
Language
Translation
bit.ly/ibmi-nodejs-watson - "How To" MCPress Article
install
git clone copies source to
the IFS from github.com

$ pwd
/home/aaron/git
$ git clone git@github.com:watson-developer-cloud/speech-to-text-nodejs.git
Cloning into 'speech-to-text-nodejs'...
remote: Counting objects: 1340, done.
Receiving objects: 95% (1273/remote: Total 1340 (delta 0)
Receiving objects: 100% (1340/1340), 7.09 MiB | 474.00 KiB/s, done.
Resolving deltas: 100% (878/878), done.
Checking connectivity... done.
Checking out files: 100% (122/122), done. npm install looks at the
$ cd speech-to-text-nodejs package.json file and installs
$ npm install dependencies from npmjs.com.
$ npm run build

npm run build "compiles" the


Javascript (combines and minify)

github.com/watson-developer-cloud/speech-to-text-nodejs
configure 4
2
1

var config = extend({


version: 'v1',
5 url: 'https://stream.watsonplatform.net/speech-to-text/api',
username: 'b5xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx09',
password: 'uxxxxxxxxxx2'
}, vcapServices.getCredentials('speech_to_text'));

bluemix.net - IBM cloud development environment


start
$ pwd
/home/aaron/git/speech-to-text-nodejs
VCAP_APP_PORT is a temporal $ VCAP_APP_PORT=8001 node app.js
environment variable that sets listening at: 8001
the listening port.

Notice mic is in use.

Side Note: Insecure


WebSockets no longer work
in Chrome. This is FireFox.

bluemix.net - IBM cloud development environment


stats

bit.ly/watson-speech-to-text-docs
stats (continued)

bluemix.net - IBM cloud development environment


websockets meets wrkactjob
Open Source: IBM i Dash

Runs on IBM i + Node.js +


WebSockets

Uses DB2 for i Services: bit.


ly/db2-for-i-services

Source code repo: bit.ly/1Z8mA8b


index.js (partial)

app.get('/wrkactjob', function (req, res) {


res.render('wrkactjob', { title: 'WRKACTJOB'})
})

setInterval( function() {
var sql = "SELECT JOB_NAME, AUTHORIZATION_NAME, " +
" ELAPSED_TOTAL_DISK_IO_COUNT, " +
" ELAPSED_CPU_PERCENTAGE " +
" FROM TABLE(QSYS2.ACTIVE_JOB_INFO()) X" +
" ORDER BY ELAPSED_CPU_PERCENTAGE DESC" +
" FETCH FIRST 20 ROWS ONLY"
db.exec(sql, function(results) {
io.emit('wrkactjob_update', results);
})
}, 2000);
wrkactjob.jade
table(id='jobs' class='table table-striped table-hover table-condensed')
thead
tr
th Job
th User
th Disk I/O
th CPU
tbody
script.
var client = io();
client.on('wrkactjob_update', function (data) {

var tbl_body = "";


$.each(data, function(k1,v1) {
var tbl_row = "";
$.each(v1, function(k,v) {
tbl_row += "<td>" + v + "</td>";
})
tbl_body += "<tr>" + tbl_row + "</tr>";
})
$("#jobs tbody").html(tbl_body);

});
We Have
Reached
The End!

Now...Get Engaged!
1. Visit litmis.com regularly for new content
2. Follow @litmisteam on Twitter
3. Contact me directly for assistance jump-
starting any open source development
projects on IBM i at
abartell@krengeltech.com

Вам также может понравиться