WebSocket Client JS with IRIS Native API as Docker Micro Server
Tags: docker, javascript, node
Published: 2021-08-18 15:29:08

 

<pre style="margin-left: 120px;">This is a coding example working on IRIS 2020.2 
It will not be kept in synch with new versions 
It is also <strong>NOT</strong> serviced by InterSystems <strong>Support</strong> !</pre>

<span style="color:#c0392b;"><big><strong>My demo video is now also available to watch the demo in operation.<br /><span style="font-size:14px;"><tt><a href="https://youtu.be/dSV-0RJ5Olg">https://youtu.be/dSV-0RJ5Olg</a></tt></span></strong></big></span>

 

Hi folks,  
It's time now for a Micro Service Demo with a total fresh IRIS Image and an image that you both PULL with  
docker  and run with <span style="background-color:#f1c40f;"></span>**<span style="background-color:#f1c40f;">only 4 lines</span>** of docker commands.   
June 1st, 2020 - rcc

There is now a compact All-in-1 version available that combines all parts in a single container image.  
For details see: [IRIS-NativeAPI-Nodejs-compact](https://openexchange.intersystems.com/package/IRIS-NativeAPI-Node-jscompact)  
May 24, 2020 - rcc

I have added a simplified installation using Docker, see context  
May 25, 2020 - rcc

There are enhanced scripts suitable & tested for Linux & Windows available here  
<https://github.com/rcemper/WSockClientMicroSV/blob/master/READMEwindows.MD>  
May 26, 2020 - rcc

This demo is a redesign of the WebSocket Client based on Node.js existing already for Caché. The major changes:

  * use of the new [IRIS Native API for Node.js](https://docs.intersystems.com/iris20192/csp/docbook/DocBook.UI.Page.cls?KEY=BJSNAT)  especially [Working with Global Arrays](https://docs.intersystems.com/iris20192/csp/docbook/Doc.View.cls?KEY=BJSNAT_globals)
  * change from a directly triggered client to a server design
  * put the result into a separate docker image as an example for a MicroService / MicroServer
  * add a simple interface in IRIS to control the MicroService execution.

<!--break-->

Instead of a utility,  you call directly on you IRIS host you now send a work-packages   
to the MicroService as would typically do with System Interoperability (aka.ENSEMBLE):  
Of course, you have the option of more than one WebSocket Server.  
Once the WebSocket Client Service has done its job you get back the result from it.

The advantage over the built-in WebSocket Client is, that all Network, Security,  
Firewall issues are kept away from the core data server. Not to talk about the  
experience and quality Node.js has in this arena.

The demo uses <wss://echo.websocket.org/> as default EchoServer  
Next, you enter some lines of text.  
At any point, you can add "Lorem Ipsum" text for more content between your own text.  
Next, you sent it to the service and wait for the echo.  
There is also the option to change your text before sending  
as Exit the control program or Stop the service.

All this processing runs asynchronously.  
Instead of waiting for completion, the Listener displays periodically  
what was received from the echo server so far.

To **install** it you need a

  * docker image for IRIS ( intersystems/iris-community:2020.2.0.199.0 )
  * docker image for the WebSocket MicroServer (<span style="font-size:16px;"><span style="color:#0000ff;"><span style="font-family:Courier New,Courier,monospace;">docker pull rcemper/rcc:demoJS</span></span></span>)
  * simplified init: just run this docker command for **init** 
          docker run --name ini1 --init -it --rm \
            -privileged -v $(pwd):/external \
            rcemper/rcc:demoJS bash /rcc/init.sh    ### The original approach is still valid but not required. ###

  * <span style="color:#95a5a6;">WSockClientMicroSV.tar.gz from Open Exchange or here to make use of </span>[<span style="color:#95a5a6;">IRIS-Docker-micro-Durability</span>](https://community.intersystems.com/user/69016/my-drafts)
  * <span style="color:#95a5a6;">check directory <strong>demo</strong>:  set  protection to <strong>rwx</strong> (chmod 777) as Docker Image is a nobody at your level.</span> ###

To **run** it start IRIS first (either -d or -it to observe the behavior) **from directory demo** (!)

<pre style="margin-left: 80px;"><code>docker run --name iris1 --init --rm -d \
-p 52773:52773 -p 51773:51773 \
-v $(pwd):/external \
intersystems/iris-community:2020.2.0.199.0 \
-b /external/pre.copy
</code></pre>

Next, the MicroServer

<pre style="margin-left: 80px;"><code>docker run --name rcc1 --init -it --rm \
rcemper/rcc:demoJS /usr/bin/node /rcc/nodejs/WSockIris.js $(hostname -I)
</code></pre>

. As you started it with **-it** you see

<pre style="margin-left: 80px;"><code>platform = linux: ubuntu  

    *****************************  
    Connect to IRIS on: 192.168.0.23  
Successfully connected to InterSystems IRIS.  
    *** wait 3sec for request ***  
    ******* Startup done ********  

    *** wait 3sec for request ***  
    *** wait 3sec for request ***  
</code></pre>

then the control application in a new Linux terminal

<pre style="margin-left: 80px;"><code>docker exec -it iris1 iris session iris ZSocket  
</code></pre>

and you see

<pre style="margin-left: 80px;"><code>*** Welcome to WebSocket Micoservice demo ***  
Known Hosts (*=Exit) [1]:  
1  wss://echo.websocket.org/  
2  --- server 2 ----  
3  --- server 3 ----  
select (1):  ==> wss://echo.websocket.org/  
#
Enter text to get echoed from WebSocketClient Service
Terminate with * at first position
or get generated text by %
or append new text with @

1    hello socket microServer
2    now you got 2 lines
3    *

Select action for WebClient Service
New EchoServer (E), Send+Listen(S),New Text(N),Exit(X), Exit+Stop Client(Z) [S]s
%%%%%%%%%%%%%%%%%%%%%%%%%%

******* 0 Replies *******

******* 2 Replies *******
1    hello socket microServer
2    now you got 2 lines


Select action for WebClient Service
</code></pre>

and on MicroService

<pre style="margin-left: 80px;"><code>*** wait 3sec for request ***
echoserver:  wss://echo.websocket.org/
** Lines to process: 1 **
********* next turn *********
* WebSocket Client connected *
****** Client is ready ****** 

Line: 1 text> 'hello socket microServer '
Received: 1 > 'hello socket microServer '

Line: 2 text> 'now you got 2 lines '
Received: 2 > 'now you got 2 lines '

******* lines sent: 2 ******
*** replies received: 2 ****

*** wait 3sec for request ***</code></pre>

<h2 style="font-style:italic;">
  <span style="color:#ff0000;">Late warning. always check the version of your image!<br />I just fell into <span style="font-size:16px;"> <span style="font-family:Courier New,Courier,monospace;">intersystems/iris-community:2020.2.0</span></span><span style="font-family:Courier New,Courier,monospace;">.</span></span><span style="color:#000000;"><span style="font-family:Courier New,Courier,monospace;"><strong>204</strong></span></span><span style="color:#ff0000;"><span style="font-family:Courier New,Courier,monospace;">.<span style="font-size:16px;"></span></span></span>
</h2>

<span style="font-size:16px;"><span class="marker"><span style="font-family:Arial,Helvetica,sans-serif;"><em><strong>NOTICE: ALL SCRIPTS ARE TESTED FOR LINUX ONLY !</strong></em></span></span></span>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v78i9arznlmday0n9s2o.png)