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!
Join the DZone community and get the full member experience.
Join For FreeI 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
Opinions expressed by DZone contributors are their own.
Comments