Using WebSockets With Apache NiFi

DZone 's Guide to

Using WebSockets With Apache NiFi

This simple WebSockets server and client is like the "Hello World" of WebSockets. It's like an echo — whatever the client sends, we send it back!

· Big Data Zone ·
Free Resource

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

This simple WebSockets server and client is like the "Hello World" of WebSockets. It's like an echo — whatever the client sends, we send it back!

My suggested use cases for WebSockets are:

  • WebSocket client to Slack interface.
  • WebSocket client to email.
  • WebSocket chat stored to Apache Phoenix.
  • WebSocket to communicate with mobile web apps.
  • WebSocket to send detailed log details from enterprise web applications directly to the log ingestion platform, bypassing the local filesystem.

Step 1HandleHTTPRequest accepts the HTTP calls from browsers.

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

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

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

Step 5ListenWebSocket is the actual WebSocket server listener; it's what our client will talk to.

Step 6PutWebSocket is the reply to the WebSocket client.

WebSockets server:

WebSockets client (static HTML5 page with Javascript) hosted on NiFi:

WebSocket conversation on the client side:

Here's a script to output the HTML5 JavaScript WebSocket client:

cat server.sh    
cat /wsclient.html    
<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...");        
};                }               
// The browser doesn't support WebSocket                  
alert("WebSocket NOT supported by your Browser!");        
}             }         
<div id="sse">             
<a href="javascript:WebSocketTest()">Run WebSocket</a>         


apache nifi, big data, data ingestion, data transfer, tutorial, websockets

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}