Skip to content

Instantly share code, notes, and snippets.

@mblackstock
Last active November 16, 2016 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mblackstock/95c50a357aabad087948598b9baa3210 to your computer and use it in GitHub Desktop.
Save mblackstock/95c50a357aabad087948598b9baa3210 to your computer and use it in GitHub Desktop.
FRED Node-RED hosted chat application with sentiment analysis

This flow uses http nodes and web sockets nodes to serve up a simple web page with a chat application that colours messages depending on whether they are positive or negative according to the sentiment node.

Copy the flow, and import it from the clipboard using Node-RED on FRED.

Change the URL ws://fred.sensetecnic.com/public/guides/receive and ws://fred.sensetecnic.com/public/guides/publish, replacing guides with your FRED user name.

Change the same URL to get it working on a 'stock' Node-RED install, or check it out on the Node-RED flows library - flows.nodered.org.

[{"id":"1df59eca.55b6f1","type":"websocket-listener","path":"/public/publish","wholemsg":"false"},{"id":"63c370e8.28cd7","type":"websocket-listener","path":"/public/receive","wholemsg":"false"},{"id":"80990680.8098c8","type":"websocket in","z":"4b9873d2.04b5fc","name":"","server":"63c370e8.28cd7","client":"","x":161,"y":133,"wires":[["6fc95961.e4f308"]]},{"id":"d2fa1095.f4364","type":"http in","z":"4b9873d2.04b5fc","name":"","url":"/public/chat","method":"get","swaggerDoc":"","x":149,"y":350,"wires":[["41920976.d1ec78"]]},{"id":"ec2882fc.c3755","type":"http response","z":"4b9873d2.04b5fc","name":"","x":343,"y":356,"wires":[]},{"id":"41920976.d1ec78","type":"template","z":"4b9873d2.04b5fc","name":"chat html","field":"payload","fieldType":"msg","format":"text","syntax":"mustache","template":"<!doctype html>\n<html lang=\"en\">\n<head>\n <meta charset=\"utf-8\">\n <title>FRED-powered chat app</title>\n <script src=\"http://code.jquery.com/jquery-1.11.3.min.js\"></script>\n <script src=\"http:///code.jquery.com/jquery-migrate-1.2.1.min.js\"></script>\n <style>\n #messages {border-color:#999; border-style:solid; width:250px; min-height:200px; margin:5px;}\n .msg {color:#FFF; background-color:#2980B9; padding:2px; margin:2px;}\n .server {color:#999; background-color:white; font-size:small;}\n .sentiment-3 {background-color:#992222;} \n .sentiment0 {background-color:#2980B9;}\n .sentiment3 {background-color:#229922;}\n #form {margin:5px;}\n #form input {width:250px;}\n </style>\n</head>\n<body>\n <div id=\"messages\"></div>\n <form id=\"form\" onsubmit=\"return false;\">\n <input id=\"text\" type=\"text\" onkeypress=\"return sendText(event)\" />\n </form>\n\n <script type=\"text/javascript\">\n\n // Open a websocket using FRED.\n var publishSocket = new WebSocket(\"ws://fred.sensetecnic.com/public/guides/receive\");\n var listenSocket = new WebSocket(\"ws://fred.sensetecnic.com/public/guides/publish\");\n\n listenSocket.onmessage = function (event) {\n // When receiving a message append a div child to #messages\n data = JSON.parse(event.data);\n $(\"#messages\").append(\"<div class='msg sentiment\"+data.sentiment+\"' >\"+data.timestamp+\" - \"+data.msg+\"</div>\")\n if ($(\"#messages\").children().length > 10 ) { $(\"#messages :first-child\").remove()}\n }\n\n listenSocket.onclose = function(event){\n $(\"#messages\").append(\"<div class='msg server'>Disconnected from server.</div>\");\n }\n\n listenSocket.onopen = function(event){\n $(\"#messages\").append(\"<div class='msg server'>Connected to server.</div>\")\n }\n\n function sendText(event) {\n // Only if return key pressed\n if (event.keyCode == 13) {\n // Construct object containing the data the server needs.\n d = new Date();\n var data = {\n msg: $(\"#text\").val(),\n timestamp: d.getHours() +\":\"+ d.getMinutes() + \":\" + d.getSeconds()\n };\n // Send the msg object as a JSON-formatted string.\n publishSocket.send(JSON.stringify(data)); \n // Blank the text input element\n $(\"#text\").val(\"\");\n }\n }\n </script>\n\n</body>\n</html>\n","x":271,"y":401,"wires":[["ec2882fc.c3755"]]},{"id":"3d636439.a8dd5c","type":"sentiment","z":"4b9873d2.04b5fc","name":"","x":363,"y":270,"wires":[["e768c89.51e6d38"]]},{"id":"1b2ea2cd.3e9b3d","type":"change","z":"4b9873d2.04b5fc","name":"","rules":[{"t":"set","p":"data","to":"msg.payload"},{"t":"set","p":"payload","to":"msg.payload.msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":285,"y":225,"wires":[["3d636439.a8dd5c"]]},{"id":"6fc95961.e4f308","type":"json","z":"4b9873d2.04b5fc","name":"","x":185,"y":265,"wires":[["1b2ea2cd.3e9b3d"]]},{"id":"e768c89.51e6d38","type":"function","z":"4b9873d2.04b5fc","name":"format message","func":"return {\n payload: {\n msg:msg.data.msg,\n timestamp:msg.data.timestamp,\n sentiment:msg.sentiment.score\n }\n};","outputs":1,"noerr":0,"x":480,"y":227,"wires":[["6b38ba8a.daf444"]]},{"id":"6b38ba8a.daf444","type":"websocket out","z":"4b9873d2.04b5fc","name":"","server":"1df59eca.55b6f1","client":"","x":627.9999694824219,"y":174.09091186523438,"wires":[]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment