+ All Categories
Home > Documents > Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets...

Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets...

Date post: 26-May-2020
Category:
Upload: others
View: 5 times
Download: 0 times
Share this document with a friend
26
Copyright 2015 Aaron Bartell Aaron Bartell Director of IBM i Innovation [email protected] Watson IBM i WebSockets
Transcript
Page 1: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

Copyright 2015 Aaron Bartell

Aaron BartellDirector of IBM i Innovation

[email protected]

Watson IBM i WebSockets

Page 2: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

● 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...

Page 3: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

agenda

What are WebSockets?

Simple WebSocket chat app

What is Watson?

Watson Speech-to-text app

WRKACTJOB and WebSockets

Page 4: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

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!

Page 5: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

why?

Low latency

Server can initiate subsequent communication

Have you used Google Docs?

docs.google.com

Page 6: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

before websockets...

bit.ly/pubnub-longpolling

… there was long polling

Page 7: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

high level

photo creds to pubnum.com

Page 8: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

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!

Page 9: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

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).

Page 10: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

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");};

Page 11: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

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

Page 12: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

browser-based dev tools

These are running on IBM i!

cloud9

ungit

Page 13: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

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

Page 14: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

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');});

Page 15: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

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>

Page 16: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

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

Page 17: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

bit.ly/ibmi-nodejs-watson - "How To" MCPress Article

Page 18: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

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)

Page 19: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

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

Page 20: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

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.

Page 21: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

bit.ly/watson-speech-to-text-docs

stats

Page 22: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

bluemix.net - IBM cloud development environment

stats (continued)

Page 23: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

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

Page 24: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

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);

Page 25: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

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);

});

Page 26: Watson IBM i WebSockets - OCEAN User Group ibm i... · 2016-04-21 · Watson IBM i WebSockets Consulting - Jumpstart your open source pursuit. Small ... - Runs on IBM i - Written

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]


Recommended