DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How AI Is Rewriting Full-Stack Java Systems: Practical Patterns with Spring Boot, Kafka and WebSockets
  • Beyond Ingestion: Teaching Your NiFi Flows to Think
  • Playwright for Real-Time Applications: Testing WebSockets and Live Data Streams
  • Creating Real-Time Dashboards Using AWS OpenSearch, EventBridge, and WebSockets

Trending

  • LLM-Powered Deep Parsing for Industrial Inventory Search
  • A System Cannot Protect What It Does Not Understand
  • 5 Common Security Pitfalls in Serverless Architectures
  • Building a RAG-Powered Bug Triage Agent With AWS Bedrock and OpenSearch k-NN
  1. DZone
  2. Coding
  3. Tools
  4. Using WebSockets With Apache NiFi

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!

By 
Tim Spann user avatar
Tim Spann
DZone Core CORE ·
Nov. 26, 17 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
16.4K Views

Join the DZone community and get the full member experience.

Join For Free

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 1: HandleHTTPRequest accepts the HTTP calls from browsers.

Step 2: ExecuteStreamCommand returns the 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. StandardHTTPContextMap is required to store HTTP requests and responses to share them through the stream.

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

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

Step 6: PutWebSocket 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    
<!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:WebSocketTest()">Run WebSocket</a>         
</div>      
</body>   
</html>  

Resources

  • Wiki page for WebSockets
  • Documentation on WebSockets
  • The WebSocket Protocol
WebSocket Apache NiFi

Opinions expressed by DZone contributors are their own.

Related

  • How AI Is Rewriting Full-Stack Java Systems: Practical Patterns with Spring Boot, Kafka and WebSockets
  • Beyond Ingestion: Teaching Your NiFi Flows to Think
  • Playwright for Real-Time Applications: Testing WebSockets and Live Data Streams
  • Creating Real-Time Dashboards Using AWS OpenSearch, EventBridge, and WebSockets

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook