X

The leading edge of scripting languages and Oracle Database brought to you by the Data Access Development team

Running Carsten Czarski's node-oracledb WebSocket Example

Christopher Jones
Senior Principal Product Manager

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-12.1.0.2.0-1.x86_64.rpm
    # rpm -ivh oracle-instantclient12.1-devel-12.1.0.2.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:

    {
    "name": "ccwebsockets",
    "version": "1.0.0",
    "description": "Carsten's WebSocket Demo application using node-oracledb 0.3.1.",
    "scripts": {

    "start": "node 05-websockets.js"
    },
    "dependencies": {

    "oracledb": "oracle/node-oracledb#v0.3.1",

    "websocket": "^1.0",

    "express": "^4.11"
    }
    }
  • Edit 05-websockets.js and change the database credentials at line 111. The schema needs to have the EMP table.

    oracledb.createPool(
    {
    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:

    > ccwebsockets@1.0.0 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.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha
Oracle

Integrated Cloud Applications & Platform Services