Tuesday, 25 July 2017

WEB SOCKETS IN ORACLE APEX
https://www.dropbox.com/s/dyulnb1qqv7r1gr/Websocket-s.rar?dl=0

1) First Copy the node_modules.rar file in C: / Drive and extract the file there.
2) Install node.js in your Local Computer.
3) Now open and type this following Command to check the Connection to node.js server :
C:\Users\Administrator>cd/
C:\>cd node_modules
C:\node_modules>node server.js
Mon Oct 10 2016 15:46:47 GMT+0500 (Pakistan Standard Time) Server is listening on port 1449
4) Now connect to sys as sysdba and run following Process :

begin
dbms_network_acl_admin.create_acl

( acl => 'utlhttp.xml'
, description => 'Http ACL'
, principal => 'APEX_050000'
, is_grant => true
, privilege => 'connect'
, start_date => null
, end_date => null
);
commit;
end;

begin
dbms_network_acl_admin.assign_acl
( acl => 'utlhttp.xml'
, host => '127.0.0.1'
, lower_port => 1499
, upper_port => 1499
);
commit;
end;

5) First of all you will have to have some sort of procedure on the database that will sent a message to your server.js …. Remember this procedure should only be run with schema you are working on. The Procedure is as follows:


Create or replace 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:1449',
p_http_method => 'POST',
p_body => '{"message":"' ||
p_text || '"}');
end;
6) Open the particular form of page and create page process and write following pl/sql code :
Begin
Push(p_text => ‘REFRESH’);
End;
7) Go to your report region and assign static id FOR example : STATIC_ID_OF_REGION and write following code in execute when page loads:
$(function(){
window.WebSocket = window.WebSocket || window.MozWebSocket;

if (!window.WebSocket) {
console.log('Sorry websockets');
}
else {
var connection = new WebSocket('ws://localhost:1449');
/*server name */
connection.onerror = function (error) {
console.log( 'Sorry, but there is some problem with your
connection or the server is down.');
};
connection.onmessage = function (message) {
console.log(message.data);
try {
var json = JSON.parse(message.data);

if (json.message=='REFRESH') {
jQuery('#STATIC_ID_OF_REGION').trigger('apexrefresh');
}
else {
console.log(json.message);
}

}
catch (e) {
console.log('Thisnot valid JSON: ' + message.data);
}
};
}
});

8) Open Windows Firewall and advance setting
Create Inbound Rules, Create Rule for Port "1449" if web socket is running on 1449 and "allow connections"
3. Give Rule Name "web socket Port" and save

Now you can access the web socket Port from another PC

and that’s it.
Mind you, this is just one implementation, but you can push every database event to the clients page.

You can also idea on the following page
https://emoracle.wordpress.com/2012/04/11/using-websockets-in-apex-for-automatic-refresh-with-nodejs/

No comments:

Post a Comment