Copyright 2015 Aaron Bartell
Aaron BartellDirector of IBM i Innovation
Watson IBM i WebSockets
● 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 [email protected]
This session brought to you by...
agenda
What are WebSockets?
Simple WebSocket chat app
What is Watson?
Watson Speech-to-text app
WRKACTJOB and WebSockets
what is a websocket?
Full-duplex communication
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
WebSocket logo. I also don't know what it means, but it sure looks cool!
why?
Low latency
Server can initiate subsequent communication
Have you used Google Docs?
docs.google.com
before websockets...
bit.ly/pubnub-longpolling
… there was long polling
high level
photo creds to pubnum.com
RequestGET ws://mydomain.com/?encoding=text HTTP/1.1Origin: http://mydomain.comCookie: __utma=99asConnection: UpgradeHost: mydomain.comSec-WebSocket-Key: uRovscZjNol/umbTt5uKmw==Upgrade: websocketSec-WebSocket-Version: 13
the handshake (raw)
Success!
ResponseHTTP/1.1 101 Switching ProtocolsUpgrade: websocketConnection: UpgradeSec-WebSocket-Accept: iXFYxLieDcAue5MC56SsA3qX8zE=Sec-WebSocket-Extensions: permessage-deflate
Success!
websocket frames
bit.ly/mozilla-websocket-server-frames
Average size of http headers alone is 800 bytes. Now we are down to only sending content(much smaller).
events
bit.ly/SO-websocket-connections
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");};
cross-domain
bit.ly/SO-websocket-connections
WebSockets is cross-domain by default
Up to you to optionally restrict domain access on server via Origin header
This space intentionally left blank
browser-based dev tools
These are running on IBM i!
cloud9
ungit
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>
bit.ly/watson-services-catalog
DocumentConversion
LanguageTranslation
Speech To Text
ToneAnalyzer
VisualRecognition
AlchemyData News
Cognitive computing is the simulation of human thought processes in a computerized model. Cognitive computing involves self-learning systems that use data mining, pattern recognition and natural language processing to mimic the way the human brain works.
Over 19 services with new ones regularly coming
bit.ly/ibmi-nodejs-watson - "How To" MCPress Article
github.com/watson-developer-cloud/speech-to-text-nodejs
install
$ pwd/home/aaron/git$ git clone [email protected]:watson-developer-cloud/speech-to-text-nodejs.gitCloning 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.$ cd speech-to-text-nodejs$ npm install$ npm run build
git clone copies source to the IFS from github.com
npm install looks at the package.json file and installs dependencies from npmjs.com.
npm run build "compiles" the Javascript (combines and minify)
bluemix.net - IBM cloud development environment
configure
var config = extend({ version: 'v1', url: 'https://stream.watsonplatform.net/speech-to-text/api', username: 'b5xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx09', password: 'uxxxxxxxxxx2'}, vcapServices.getCredentials('speech_to_text'));
12
3
4
5
bluemix.net - IBM cloud development environment
start
$ pwd/home/aaron/git/speech-to-text-nodejs$ VCAP_APP_PORT=8001 node app.jslistening at: 8001
VCAP_APP_PORT is a temporal environment variable that sets the listening port.
Notice mic is in use.
Side Note: Insecure WebSockets no longer work in Chrome. This is FireFox.
bit.ly/watson-speech-to-text-docs
stats
bluemix.net - IBM cloud development environment
stats (continued)
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.jadetable(id='jobs' class='table table-striped table-hover table-condensed') thead tr th Job th User th Disk I/O th CPU tbodyscript. 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 content2. Follow @litmisteam on Twitter3. Contact me directly for assistance jump-
starting any open source development projects on IBM i at [email protected]