Log Push Notifications with WebSockets to MongoDB

This flow creates a "POST /chat" endpoint that triggers a Push Notification to a WebSockets endpoint. When the Push Notification is received the message is logged to a MongoDB instance. The "GET /log" endpoint reads the logs from the MongoDB and sends the response.

[{"id":"c1d7c309.6d608","type":"websocket in","z":"174a1c7f.bbd374","name":"/ws","server":"db40e212.852b3","client":"","x":110,"y":100,"wires":[["fe06e119.38e17"]]},{"id":"c60aabb7.f7bdb8","type":"websocket out","z":"174a1c7f.bbd374","name":"Send Push Notification","server":"","client":"feaeafe.d98475","x":604,"y":247,"wires":[]},{"id":"f9c2d90a.fc7418","type":"http in","z":"174a1c7f.bbd374","name":"","url":"/chat","method":"get","upload":false,"swaggerDoc":"","x":120,"y":247,"wires":[["a667f883.fa7168","4da09c06.3b4a54"]]},{"id":"64a472de.962d6c","type":"comment","z":"174a1c7f.bbd374","name":"Push Notication Client ","info":"","x":160.5,"y":60,"wires":[]},{"id":"4627421e.6239bc","type":"comment","z":"174a1c7f.bbd374","name":"Push Notification Server","info":"","x":170.5,"y":209,"wires":[]},{"id":"a667f883.fa7168","type":"debug","z":"174a1c7f.bbd374","name":"log2-1","active":true,"console":"false","complete":"payload","x":274,"y":294,"wires":[]},{"id":"4da09c06.3b4a54","type":"template","z":"174a1c7f.bbd374","name":"Create Push Notification Msg","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"You said: {{payload.msg}} !","output":"str","x":342,"y":247,"wires":[["c60aabb7.f7bdb8","951877c0.164028"]]},{"id":"7cef9a5a.736eb4","type":"http response","z":"174a1c7f.bbd374","name":"","statusCode":"","headers":{},"x":812,"y":294,"wires":[]},{"id":"8f46ce96.5932d","type":"comment","z":"174a1c7f.bbd374","name":"save all received push notifications to mongodb","info":"","x":564.5,"y":60,"wires":[]},{"id":"b0bd5cd8.29a9e","type":"comment","z":"174a1c7f.bbd374","name":"Send Push Notification upon GET /chat trigger","info":"","x":572.5,"y":207,"wires":[]},{"id":"84800d5c.80c1a","type":"comment","z":"174a1c7f.bbd374","name":"Read Chatlog","info":"","x":132.5,"y":344,"wires":[]},{"id":"7a6ed3a8.8bdbbc","type":"http in","z":"174a1c7f.bbd374","name":"GET /log","url":"/log","method":"get","upload":false,"swaggerDoc":"","x":121.5,"y":386,"wires":[["3348cf88.7caa3"]]},{"id":"fe06e119.38e17","type":"function","z":"174a1c7f.bbd374","name":"Parse Log Msg","func":"msg.payload = {'msg': msg.payload};\nreturn msg;","outputs":1,"noerr":0,"x":275,"y":100,"wires":[["98720683.4d0738"]]},{"id":"b03081cb.f68dc","type":"http response","z":"174a1c7f.bbd374","name":"","statusCode":"","headers":{},"x":487.5,"y":385,"wires":[]},{"id":"32c75ad5.afefd6","type":"debug","z":"174a1c7f.bbd374","name":"log3-1","active":true,"console":"false","complete":"payload","x":488,"y":423,"wires":[]},{"id":"3348cf88.7caa3","type":"mongodb2 in","z":"174a1c7f.bbd374","service":"_ext_","configNode":"1dbf1fc5.83c6a","name":"mongodb:find","collection":"chatlog","operation":"find.toArray","x":303.5,"y":385,"wires":[["32c75ad5.afefd6","b03081cb.f68dc"]]},{"id":"98720683.4d0738","type":"mongodb2 in","z":"174a1c7f.bbd374","service":"_ext_","configNode":"1dbf1fc5.83c6a","name":"mongodb:insert","collection":"chatlog","operation":"insert","x":465,"y":100,"wires":[["523b5ffb.7a554"]]},{"id":"523b5ffb.7a554","type":"debug","z":"174a1c7f.bbd374","name":"log1-1","active":true,"console":"false","complete":"true","x":632.5,"y":144,"wires":[]},{"id":"951877c0.164028","type":"template","z":"174a1c7f.bbd374","name":"Create HTTP Response","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Sent a Push Notification: {{payload}} !","output":"str","x":616,"y":294,"wires":[["7cef9a5a.736eb4"]]},{"id":"db40e212.852b3","type":"websocket-listener","z":"","path":"/ws","wholemsg":"true"},{"id":"feaeafe.d98475","type":"websocket-client","z":"","path":"ws://username-nodered.mybluemix.net/ws","wholemsg":"false"},{"id":"1dbf1fc5.83c6a","type":"mongodb2","z":"","uri":"mongodb://username-cosmosdb:[email protected]:10255/?ssl=true&replicaSet=globaldb","name":"azure-mongodb","options":"","parallelism":"-1"}]
remkohdev

Flow Info

created 1 week, 6 days ago

Node Types

Core
  • comment (x5)
  • debug (x3)
  • function (x1)
  • http in (x2)
  • http response (x2)
  • template (x2)
  • websocket in (x1)
  • websocket out (x1)
  • websocket-client (x1)
  • websocket-listener (x1)
Other

Tags

  • mongodb
  • websocket
  • push-notifications
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option