Community Articles
Find and share helpful community-sourced technical articles
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.
Labels (1)
Super Guru

Setting up a Websocket Client and Server with Apache NiFi 1.1.

I wanted to test out the new WebSocket Listener in Apache NiFi 1.1, but I needed a server to serve up my HTML client. So I ran that web server with NiFi as well. So my full solution is hosted and runs through Apache NiFi.

This simple WebSockets Server and Client does the hello world of web sockets, Echo! Whatever the client sends, we send it back.

My Suggested Use Cases for WebSockets

  • WebSocket Client to Slack Interface
  • WebSocket Client to Email
  • WebSocket Chat stored to Apache Phoenix
  • WebSocket To Communicate From Mobile Web Apps
  • WebSocket To Send Detailed Log Details From Enterprise Web Applications Directly To Log Ingest Platform, bypassing local filesystem.

10711-wsflow.png

Step 1: HandleHTTPRequest This accepts the HTTP calls from browsers

10712-wshandlerequest.png

Step 2: ExecuteStreamCommand Returns HTML page (could do getfile or any number of other ways of getting the HTML as a flowfile)

Step 3: HandleHttpResponse this serves up our web page to browsers. A StandardHTTPContextMap is required to store HTTP requests and responses to share them through the stream.

10714-wshandlehttpresponse.png

10715-wsstandardhttpcontextmap.png

Step 4: PutFile Just to keep logs of what's going on, I saw all the flow files to the local file system.

Step 5: ListenWebSocket This is the actual web socket server listener, it is what our client will talk to.

10716-wslistenwebsocket.png

10717-wsjettywebsocketserver.png

Step 6: PutWebSocket This is the reply to the web socket client.

10718-wsputwebsocket.png

Web Sockets Server

10722-wsresults.png

10723-wsmessage.png

10724-wsresult3.png

Web Sockets Client (Static HTML5 Page with Javascript) Hosted on NiFi

10719-wswebpage.png

Web Socket Conversation On the Client Side

10720-wswebsocketmsgsent.png

10721-wsmsgreceived.png

A Shell Script to Output The HTML5 Javascript WebSocket Client

➜  nifi-1.1.0.2.1.1.0-2 cat server.sh
cat /Volumes/Transcend/Apps/nifi-1.1.0.2.1.1.0-2/wsclient.html
➜  nifi-1.1.0.2.1.1.0-2 cat wsclient.html
<!DOCTYPE HTML>
<html>
   <head>
      <script type="text/javascript">
         function WebSocketTest()
         {
            if ("WebSocket" in window)
            {
               alert("WebSocket is supported by your Browser!");
               // Let us open a web socket
               var ws = new WebSocket("ws://localhost:9998/echo");
                  ws.send("MSG:  NIFI IS AWESOME");
               ws.onopen = function()
               {
                  // Web Socket is connected, send data using send()
                  ws.send("Message to send");
                  alert("Message is sent...");
               };
               ws.onmessage = function (evt)
               {
                  var received_msg = evt.data;
                  alert("Message is received...");
               };
               ws.onclose = function()
               {
                  // websocket is closed.
                  alert("Connection is closed...");
               };
            }
            else
            {
               // The browser doesn't support WebSocket
               alert("WebSocket NOT supported by your Browser!");
            }
         }
      </script>
   </head>
   <body>
      <div id="sse">
         <a href="javascript&colon;WebSocketTest()">Run WebSocket</a>
      </div>
   </body>
</html>

Construct a Visual Web Server to Server up Static HTML 5 WebSocket Client Page

Listen For Websocket

https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi.processors.websocket.ListenWebSock...

Put Message Back

https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi.processors.websocket.PutWebSocket/...

Reference:

NiFi Template (This is for Apache NiFi 1.1.x)

websockets.xml

5,989 Views
Don't have an account?
Coming from Hortonworks? Activate your account here
Version history
Revision #:
2 of 2
Last update:
‎08-17-2019 07:02 AM
Updated by:
 
Contributors
Top Kudoed Authors