My colleague Carsten Czarski recently presented on the node-oracledb driver for Node.js. One of his demos used WebSockets. It was a live demo, not captured in slides. I thought I'd explain how I got it to run in my Oracle Linux 64 bit environment.
Download and extract the Node 0.10.36 bundle from here. (At time of writing, the node-oracle driver requires Node.js 0.10). Add the bin to your PATH, for example:
$ export PATH=/opt/node-v0.10.36-linux-x64/bin:$PATH
Download and install the 'basic' and 'devel' Instant Client RPMs from OTN:
# rpm -ivh oracle-instantclient12.1-basic-126.96.36.199.0-1.x86_64.rpm
# rpm -ivh oracle-instantclient12.1-devel-188.8.131.52.0-1.x86_64.rpm
Download Carsten's demo code from here and extract it:
$ cd /home/cjones
$ mkdir wsdemo
$ cd wsdemo
$ mv $HOME/Downloads/nodejs-beispielprogramme.zip .
$ unzip nodejs-beispielprogramme.zip
Create a new package.json file:
"description": "Carsten's WebSocket Demo application using node-oracledb 0.3.1.",
"start": "node 05-websockets.js"
Edit 05-websockets.js and change the database credentials at line 111. The schema needs to have the EMP table.
user : "scott",
password : "tiger",
connectString : "localhost/pdborcl",
poolMin : 5,
poolMax : 10
Also in 05-websockets.js, change the path name at line 65 to your current directory name:
filename = path.join("/home/cjones/wsdemo", uri);
Use npm to automatically install the node-oracle driver and the "websocket" and "express" dependencies listed in package.json:
$ npm install
To run the demo, use the package.json script "start" target to load 05-websockets.js:
$ npm start
The server will start:
> firstname.lastname@example.org start /home/cjones/wsdemo
> node 05-websockets.js
Websocket Control Server listening at http://0.0.0.0:9000
Database connection pool established
Open a couple of browser windows to http://127.0.0.1:9000/html/websocket.html. These are the clients listening for messages.
The output is the starting point of the demo. Let's send a message to those clients.
Open a third browser window for the URL http://127.0.0.1:9000/update/CLARK. The two listening windows will be updated with the "message" containing the query result payload. My screenshot shows this, and also has evidence that I had previously visited http://127.0.0.1:9000/update/KING :
You might have noticed the screen shots were made on OS X. If you are not on Linux, refer to INSTALL to see how to install Node.js and node-oracledb. The package.json file I created will download node-oracledb 0.3.1 so you don't need to manually get it from GitHub. You will have to set OCI_LIB_DIR and OCI_INC_DIR during installation, and then set LD_LIBRARY_PATH, DYLD_LIBRARY_PATH or PATH when you want to run node.
You can follow Carsten at @cczarski.