Learn How to Create TCP Client and Server using Node Red

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we create a simple TCP client which can dynamically switch between two different servers we use an inject node to send data to the TCP request and connect the debug on its output the payload we send is The Verge test in the TCP request we set the host to localhost and the port to 2000. if you want to connect to another device you need to swap localhost with the IP address of your device before trying out the client we need to create the TCP server for that we create a new flow and call it server the first step in creating a server is the TCP in node here we want to listen on Port 2000. we use a debug for the outgoing message in the debug we select complete message object to show all the data you make a request using our client we see the received data in the debug window we also see that we do not get an answer from the server in the message object we see the IP address of the sender and the port used to send the request in payload we see the send data displayed as a buffer when clicking on the payload the node red editor automatically converts the text to a string to also the client we need the TCP out node there we simply select reply to TCP this option uses the host and Port provided in the message object to alter the client now we see that we get an answer from the server but the text is not a string to convert the buffer to a string we can use a function node here we simply use the tostring method and set ASCII as a parameter this converts our buffer to an ASCII string the text is now displayed as a string in our debug window on the server side we can use a change to change the incoming payload to something else here we use DC server 2000 to make sure we are connected to the correct server using the return settings of the TCP request we can remove the need to convert the string using a function node now let's take a look at the close settings the closed setting is used to make sure no package is lost during the request one common misconception is that the timeout starts right after sending the request however the timeout starts after the client receives the first bit of data if the timeout runs out and the client does not have the full answer it will resend its request when sending a string you can also set the close method to when character is received if we set this character to T which is the first character of our string we only receive the letter t using this option the TCP connection closes after finding the character specified in the settings when you want to make sure you received an entire sentence we can use the new line character for example to send the Newland character we need a function node the next option is to close after fixed amount of characters this can only be used if you know the length of the answer in advance important to note here is that characters such as new line are also counted foreign is bigger than the actual length of the string the request will never close foreign the last two options are never and immediately the first option is used when you want to send data all the time and the second one is if you don't expect or don't want any answer from the server in the TCP node you can also set the output to be a string foreign now we create the TCP request which can dynamically connect to different TCP servers for that we need an inject node where resets the port and the host property here we use localhost 2000 again in the TCP request we just leave the text Fields empty as you see it is not possible to send no payload so we set one in the inject we see that the connection works just like before now we create a new server which listens on Port 2001. we can also remove the new line characters since we don't need it anymore to select the different TCP servers we use the dashboard drop down I like to use dashboards to test out Dynamic code because it is very easy to try out all the possibilities without changing the code all the time in the drop down we want to make sure that the values are 2000 and 2001. we use a change to set the value of the msg.port to msg. payload additionally we set the host to localhost by connecting the TCP output to a text output we can directly see the server's answer on the dashboard as we see our client switches between the servers as we wanted it to this is it for this week's video I hope you enjoyed it and you could learn something
Info
Channel: RuWindustries
Views: 10,492
Rating: undefined out of 5
Keywords: Node-Red, Dashboard, Industry 4.0, IOT
Id: Ix8_XqHvu9c
Channel Id: undefined
Length: 10min 11sec (611 seconds)
Published: Sat Jan 14 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.