pushing oracle coherence events to web browsers using html 5 … · 2020-06-11 · © c2b2...

17
© C2B2 Consulting Limited 2012 All Rights Reserved Pushing Oracle Coherence Events to Web Browsers using HTML 5 WebSockets and Kaazing Steve Millidge

Upload: others

Post on 21-Jun-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pushing Oracle Coherence Events to Web Browsers using HTML 5 … · 2020-06-11 · © C2B2 Consulting Limited 2012 All Rights Reserved Pushing Oracle Coherence Events to Web Browsers

© C2B2 Consulting Limited 2012 All Rights Reserved

Pushing Oracle Coherence

Events to Web Browsers

using HTML 5 WebSockets

and Kaazing

Steve Millidge

Page 2: Pushing Oracle Coherence Events to Web Browsers using HTML 5 … · 2020-06-11 · © C2B2 Consulting Limited 2012 All Rights Reserved Pushing Oracle Coherence Events to Web Browsers

© C2B2 Consulting Limited 2012 All Rights Reserved

Agenda

• What is Web Sockets

• Review Coherence Events

• Demo Configuration

• Code

Page 3: Pushing Oracle Coherence Events to Web Browsers using HTML 5 … · 2020-06-11 · © C2B2 Consulting Limited 2012 All Rights Reserved Pushing Oracle Coherence Events to Web Browsers

© C2B2 Consulting Limited 2012 All Rights Reserved

Web Sockets

• Enables Full Duplex Communication

between a browser and a Server

• Allows Web Servers to push updates to

browsers

• Better than “long polling”

• Establishes a Dedicated Socket to the

Backend Web Socket Server

Page 4: Pushing Oracle Coherence Events to Web Browsers using HTML 5 … · 2020-06-11 · © C2B2 Consulting Limited 2012 All Rights Reserved Pushing Oracle Coherence Events to Web Browsers

© C2B2 Consulting Limited 2012 All Rights Reserved

Server Sent Events

• Sent by the Server to the Browser

– Uni-directional

• Sent over standard HTTP

– Content Type is text/event-stream

• Supports only Text

• Easier to fallback to Long Polling

• Browser handles reconnect

Page 5: Pushing Oracle Coherence Events to Web Browsers using HTML 5 … · 2020-06-11 · © C2B2 Consulting Limited 2012 All Rights Reserved Pushing Oracle Coherence Events to Web Browsers

© C2B2 Consulting Limited 2012 All Rights Reserved

Web Sockets and Standards

• This is all in flux

• Rfc 6455 defines the

protocol

• W3C SSE

http://dev.w3.org/html

5/eventsource/

• W3C WebSockets

http://dev.w3.org/html

5/websockets/

Page 6: Pushing Oracle Coherence Events to Web Browsers using HTML 5 … · 2020-06-11 · © C2B2 Consulting Limited 2012 All Rights Reserved Pushing Oracle Coherence Events to Web Browsers

© C2B2 Consulting Limited 2012 All Rights Reserved

WebSocket Protocol

Client

GET /chat HTTP/1.1

Host: server.example.com

Upgrade: websocket

Connection: Upgrade

Sec-WebSocket-Key:

dGhlIHNhbXBsZSBub25jZQ==

Origin: http://example.com

Sec-WebSocket-Protocol:

chat, superchat

Sec-WebSocket-Version: 13

Server

HTTP/1.1 101 Switching

Protocols Upgrade: websocket

Connection: Upgrade Sec-

WebSocket-Accept:

s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

Sec-WebSocket-Protocol: chat

Page 7: Pushing Oracle Coherence Events to Web Browsers using HTML 5 … · 2020-06-11 · © C2B2 Consulting Limited 2012 All Rights Reserved Pushing Oracle Coherence Events to Web Browsers

© C2B2 Consulting Limited 2012 All Rights Reserved

JS API

Web Socket

WebSocket(location,protocol)

Function onmessage

Function onopen

Function onclose

Function onerror

close()

send(data)

Server Sent Events

EventSource(location)

Function onmessage

Function onopen

Function onerror

Page 8: Pushing Oracle Coherence Events to Web Browsers using HTML 5 … · 2020-06-11 · © C2B2 Consulting Limited 2012 All Rights Reserved Pushing Oracle Coherence Events to Web Browsers

© C2B2 Consulting Limited 2012 All Rights Reserved

Coherence Events Subsystem

Application

Cache

Application

Cache

Application

Cache

Application

Cache Map

Listener

Page 9: Pushing Oracle Coherence Events to Web Browsers using HTML 5 … · 2020-06-11 · © C2B2 Consulting Limited 2012 All Rights Reserved Pushing Oracle Coherence Events to Web Browsers

© C2B2 Consulting Limited 2012 All Rights Reserved

Events Code

MapListener listener = new MultiplexingMapListener() {

public void onMapEvent(MapEvent evt) {

// do something with the trader

}

};

NamedCache mapTrades = ...

Filter filter =

new AndFilter(new EqualsFilter("getTrader", traderid),

new EqualsFilter("getStatus", Status.OPEN));

mapTrades.addMapListener(listener, new

MapEventFilter(filter), true);

Page 10: Pushing Oracle Coherence Events to Web Browsers using HTML 5 … · 2020-06-11 · © C2B2 Consulting Limited 2012 All Rights Reserved Pushing Oracle Coherence Events to Web Browsers

© C2B2 Consulting Limited 2012 All Rights Reserved

Events

• E_ALL

– All Events

• E_INSERTED

– Inserts

• E_DELETED

– Deletes

• E_UPDATED

– Updated

• E_UPDATED_ENTERED

– Updated and now matched

• E_UPDATED_LEFT

– Updated and now not

matched

• E_UPDATED_WITHIN

– Updated still matched

• E_KEYSET

– All updated which change

the matching set

Page 11: Pushing Oracle Coherence Events to Web Browsers using HTML 5 … · 2020-06-11 · © C2B2 Consulting Limited 2012 All Rights Reserved Pushing Oracle Coherence Events to Web Browsers

© C2B2 Consulting Limited 2012 All Rights Reserved

Asynch Push to the UI

Applicati

on Cach

e

Applicati

on Cach

e

Applicati

on Cach

e

Applicati

on Cach

e

Applicati

on Cach

e

Applicati

on Cach

e

Applicati

on Cach

e

Applicati

on Cach

e

Applicati

on Cach

e

Applicati

on Cach

e

Applicati

on Cach

e

Applicati

on Cach

e

Applicati

on Cach

e

Applicati

on Cach

e

Applicati

on Cach

e

Applicati

on Cach

e

Applicati

on Cach

e

Applicati

on Cach

e

Applicati

on Cach

e

Applicati

on Cach

e

Applicati

on Cach

e

Price

Update

ORCL

ORCL ORCL

Page 12: Pushing Oracle Coherence Events to Web Browsers using HTML 5 … · 2020-06-11 · © C2B2 Consulting Limited 2012 All Rights Reserved Pushing Oracle Coherence Events to Web Browsers

© C2B2 Consulting Limited 2012 All Rights Reserved

Demo Setup

Internet

Applicati

on Cach

e

Applicati

on Cach

e

Coherence Grid Coherence

Client

Update

KAAZING

HTML 5

Gateway

Page 13: Pushing Oracle Coherence Events to Web Browsers using HTML 5 … · 2020-06-11 · © C2B2 Consulting Limited 2012 All Rights Reserved Pushing Oracle Coherence Events to Web Browsers

© C2B2 Consulting Limited 2012 All Rights Reserved

Coherence Initialisation

String remoteURL = args[0];

NamedCache cache =

CacheFactory.getCache("conquery");

KaazingStockClient client = new

KaazingStockClient(remoteURL, "conquery");

cache.addMapListener(new StockMapListener(client));

Page 14: Pushing Oracle Coherence Events to Web Browsers using HTML 5 … · 2020-06-11 · © C2B2 Consulting Limited 2012 All Rights Reserved Pushing Oracle Coherence Events to Web Browsers

© C2B2 Consulting Limited 2012 All Rights Reserved

Map Listener

@Override

public void entryUpdated(MapEvent event) {

if (event.getNewValue() instanceof Stock) {

System.out.println("Stock : " + event.getKey() + " has been

updated");

client.pushStockUpdate();

}

}

Page 15: Pushing Oracle Coherence Events to Web Browsers using HTML 5 … · 2020-06-11 · © C2B2 Consulting Limited 2012 All Rights Reserved Pushing Oracle Coherence Events to Web Browsers

© C2B2 Consulting Limited 2012 All Rights Reserved

Push to Kaazing

// Convert to JSON

JSONArray stocksJson = new JSONArray();

stocksJson.addAll(stocks);

// Push via UDP to KAAZING

URI remoteURI = URI.create(remoteURL);

SocketAddress remoteAddress = new

InetSocketAddress(remoteURI.getHost(),

remoteURI.getPort());

DatagramSocket socket = new DatagramSocket();

byte[] buf = message.getBytes();

DatagramPacket packet = new DatagramPacket(buf,

buf.length, remoteAddress);

socket.send(packet);

Page 16: Pushing Oracle Coherence Events to Web Browsers using HTML 5 … · 2020-06-11 · © C2B2 Consulting Limited 2012 All Rights Reserved Pushing Oracle Coherence Events to Web Browsers

© C2B2 Consulting Limited 2012 All Rights Reserved

Graph JavaScript

var eventSource = new EventSource("http://ec2-46-137-53-

185.eu-west-1.compute.amazonaws.com:8001/sse");

eventSource.onmessage = function(event) {

var array = JSON.parse(event.data);

for (var i = 0; i < array.length; i++) {

var object = array[i];

var x = (new Date()).getTime();

var y = object.price;

document.chart.series[i].addPoint([x,y],true,true);

}

}

Page 17: Pushing Oracle Coherence Events to Web Browsers using HTML 5 … · 2020-06-11 · © C2B2 Consulting Limited 2012 All Rights Reserved Pushing Oracle Coherence Events to Web Browsers

© C2B2 Consulting Limited 2012 All Rights Reserved

Thank you