Using websockets in APEX for automatic refresh with nodejs _ emoracle1
Using websockets in APEX for automatic refresh with nodejs _ emoracle1
Apr
11 Using websockets in APEX for automatic refresh 12
with nodejs
In my former post Using websockets in APEX for automatic refresh I
explained how to implement a mechanism with an external
websocketserver to push a refresh to an APEX page. It used Beaconpush.
Install node.js
Install websocket.
Node.js works with modules that can be installed. For this example we need
the websocket module. To install, use the NPM executable
"use strict";
process.title = 'database-chat';
var config = {
port:1337
};
/*
We need a HTTP-server and a websocketserver
*/
var webSocketServer = require('websocket').server;
var http = require('http');
/*
We need to keep record of connected clients
*/
var clients = [ ];
/*
The HTTP server
*/
var server = http.createServer( function(req, resp){
var body = '';
var json;
req.setEncoding('utf8');
resp.writeHead(200,{'Content-Type':'application/json' });
req.on('data', function(data) {
body += data.toString();
/*
When we get a stringified JSON-object, that's oke.
If not, we just create one with one attribute
*/
try {
json = body;
var test = JSON.parse(json);
}
catch (e) {
json = JSON.stringify({ 'message':body} );
}
/*
Push the JSON-string to all the connected clients
*/
for (var i=0; i < clients.length; i++) {
clients[i].sendUTF(json);
}
resp.end(JSON.stringify({ message: 'Send to '+ clients.le
});
});
/*
WebSocket server
*/
var wsServer = new webSocketServer({
// De WebSocket server is tied to a HTTP server.
httpServer: server
});
connection.on('close', function(connection) {
console.log((new Date()) + " Peer " + connection.
clients.splice(index, 1);
});
});
First of all you will have to have some sort of procedure on the database
that will sent a message to your server.js
procedure push
( p_text in varchar2
)
is
l_clob clob;
begin
l_clob := APEX_WEB_SERVICE.MAKE_REST_REQUEST
( p_url => 'http://127.0.0.1:1337'
, p_method => 'POST'
, p_body => '{"message":"'||p_text||'"}'
);
end;
With such a procedure you can e.g. push the message REFRESH to you
page and refresh the region.
Create a page with an interactive report that you want to be refreshed.
In the page footer add the following script:
$(function(){
window.WebSocket = window.WebSocket || window.MozWebSocket;
if (!window.WebSocket) {
console.log('Sorry, no websockets');
}
else {
var connection = new WebSocket('ws://127.0.0.1:1337');
if (json.message=='REFRESH') {
gReport.pull();
}
else {
console.log(json.message);
}
}
catch (e) {
console.log('This is not valid JSON: ' + message.data
}
};
}
});
Mind you, this is just one implementation, but you can push every database
event to the clients page.
Joe Upshaw
— 14 FEBRUARY 2013 AT 15:12
One small item that I noted…not sure of your APEX version but, under
4.2.1.00.08, the name of the method argumnet to
APEX_WEB_SERVICE.MAKE_REST_REQUEST is p_http_method rather than
p_method.
Reply
dopple
— 18 DECEMBER 2013 AT 14:58
Thanks Joe. This sorted my issue. I am using apex 4.1 with the
restful web services patch.
Reply
Joe Upshaw
— 14 FEBRUARY 2013 AT 18:54
Great article! This was exactly what we needed and, given the nature of our
business, we wouldn’t have been able to employ connections to a third
party. Thanks for sharing.
Reply
Brian
— 18 FEBRUARY 2014 AT 16:08
This is great!!! Thank you for posting this. Exactly what I needed and it
works perfectly.
Reply
Brian
— 19 FEBRUARY 2014 AT 17:32
Brian
— 19 FEBRUARY 2014 AT 17:53
I have not tried this code (because I need my own id for other
reasons). I got this from looking at the sample code of
websockets: https://github.com/Worlize/WebSocket-
Node/blob/master/test/libwebsockets-test-server.js
Brian
— 20 FEBRUARY 2014 AT 05:14
Just wanted to update… it does look like there is a problem in the onclose
function.
Also doing what is in the example code does not work: var index =
clients.indexOf(connection); so test this carefully you go this route. This
appears to never find the index.
In onClose do:
delete sockets[id];
Then instead of using for loops use:
Mario
— 30 AUGUST 2017 AT 17:35
Hi,
I got this error when I call the “PUSH” function:
Reply
magazinecrm
— 24 JANUARY 2018 AT 20:40
Dear Edwin,
when i run server.js i encounter the following error
C:\emoracle>node servera.js
C:\emoracle\servera.js:29
req.on(‘dataunction(data) {
^^^^^^^^^^^^^^^^^^^^
kindly guide..
Reply
Reply
magazinecrm
— 25 JANUARY 2018 AT 05:06
Steve
— 28 JULY 2021 AT 00:23
How does client communicate with the database? For example, if I wanted
to store a list of connected clients in the database so that messages can
then be sent to specific clients?
Reply
Leave a comment
This site uses Akismet to reduce spam. Learn how your comment data is
processed.
← Disable Oracle 11g ACL
Oracle Native web services
and SOAPUI →
Edwin van Meerendonk, Oracle Certified Developer and -Java Developer, also an
Oracle Certified DBA
Lately, heavily interested and working with Node.js, Angular, React, OracleJet, just
because it's fun.
Edwin lives with his wife and his son in Prinsenbeek, The Netherlands.
RECENT POSTS
CATEGORIES
General (16)
Javascript (4)
Non-Oracle (6)
Oracle (28)
APEX (16)
DBA (9)
PL/SQL and SQL (9)
QMS/Headstart (1)
Uncategorized (1)
ARCHIVES
September 2021
September 2019
March 2017
December 2016
July 2016
June 2016
March 2016
July 2015
May 2015
March 2015
November 2014
September 2014
March 2014
February 2014
October 2013
September 2013
August 2013
April 2013
February 2013
November 2012
September 2012
August 2012
June 2012
May 2012
April 2012
March 2012
February 2012
INFO
Create account
Log in
Entries feed
Comments feed
WordPress.com