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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot
  • Jakarta WebSocket Essentials: A Guide to Full-Duplex Communication in Java
  • Leveling Up My GraphQL Skills: Real-Time Subscriptions
  • WebSocket vs. Server-Sent Events: Choosing the Best Real-Time Communication Protocol

Trending

  • Microsoft Azure Synapse Analytics: Scaling Hurdles and Limitations
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • Unlocking the Potential of Apache Iceberg: A Comprehensive Analysis
  • Create Your Own AI-Powered Virtual Tutor: An Easy Tutorial
  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.1K 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

  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot
  • Jakarta WebSocket Essentials: A Guide to Full-Duplex Communication in Java
  • Leveling Up My GraphQL Skills: Real-Time Subscriptions
  • WebSocket vs. Server-Sent Events: Choosing the Best Real-Time Communication Protocol

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!