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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • WebSocket Debugging Without a Proxy — A Browser-First Workflow
  • 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

Trending

  • PostgreSQL vs Oracle: When Higher Cost Means Better Performance
  • Java 8 Optional - Avoid Null and NullPointerException Altogether - and Keep It Pretty
  • When One Docker Swarm Replica Became Unreachable Over an Overlay Network: A Production Case With Traefik
  • AI-Driven Integration in Large-Scale Agile Environments

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
DZone Core CORE ·
Nov. 26, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
16.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • WebSocket Debugging Without a Proxy — A Browser-First Workflow
  • 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

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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