Websocket message to single client
Sample flow of how to send messages to individual websocket clients when multiple clients are connected to the same url endpoint.
Clients send a login/logout message along with their username which is then stored in the flow context along with their session details.
Messages can then be sent to clients using the inject nodes, the funciton looks up the user in the context based on msg.user property and then sets the correct session.
If no session is found the message is dropped (otherwise empty session would be sent to all cients.
[{"id":"280155ee0a15e779","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"ec50067f31a5829d","type":"websocket in","z":"280155ee0a15e779","name":"","server":"3cafc110ca4b0d9e","client":"","x":550,"y":220,"wires":[["77c018df6b274ebe","463bbae1d88173be"]]},{"id":"48cb520ca5a1b055","type":"websocket out","z":"280155ee0a15e779","name":"","server":"3cafc110ca4b0d9e","client":"","x":1050,"y":460,"wires":[]},{"id":"884ae9ad74d3a271","type":"function","z":"280155ee0a15e779","name":"Store Session","func":"flow.set(msg.payload.user, msg._session) // Store user to session id\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1060,"y":200,"wires":[[]]},{"id":"463bbae1d88173be","type":"debug","z":"280155ee0a15e779","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":790,"y":140,"wires":[]},{"id":"77c018df6b274ebe","type":"json","z":"280155ee0a15e779","name":"","property":"payload","action":"","pretty":false,"x":710,"y":220,"wires":[["8e1ebde8718075c9"]]},{"id":"8e1ebde8718075c9","type":"switch","z":"280155ee0a15e779","name":"","property":"payload.action","propertyType":"msg","rules":[{"t":"eq","v":"login","vt":"str"},{"t":"eq","v":"logout","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":850,"y":220,"wires":[["884ae9ad74d3a271"],["58e1ac6a1156493e"]]},{"id":"097dfa095a7f0806","type":"inject","z":"280155ee0a15e779","name":"Send to a","props":[{"p":"payload"},{"p":"user","v":"a","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":560,"y":440,"wires":[["01b34fdca9fd28c9"]]},{"id":"03dffafbc6737442","type":"inject","z":"280155ee0a15e779","name":"Send to b","props":[{"p":"payload"},{"p":"user","v":"b","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":560,"y":500,"wires":[["01b34fdca9fd28c9"]]},{"id":"01b34fdca9fd28c9","type":"function","z":"280155ee0a15e779","name":"Set Session","func":"msg._session = flow.get(msg.user)\nif (msg._session){\n return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":830,"y":460,"wires":[["48cb520ca5a1b055"]]},{"id":"58e1ac6a1156493e","type":"function","z":"280155ee0a15e779","name":"Store Session","func":"flow.set(msg.payload.user) // Clear user session id\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1060,"y":260,"wires":[[]]},{"id":"b21a266c4615801c","type":"comment","z":"280155ee0a15e779","name":"Login Message","info":"Client needs to send a login message in the following format:\n`{\"action\" : \"login\", \"user\" : \"a\"}`\nAnd the following to logout\n`{\"action\" : \"logout\", \"user\" : \"a\"}`\n\nhttps://websocketking.com/ Is a useful test client","x":560,"y":160,"wires":[]},{"id":"3cafc110ca4b0d9e","type":"websocket-listener","path":"/socket","wholemsg":"false"}]