I wanted to make a simple chat where users can change the color of the chat box by entering in hex colors. I think colors can convey a lot of emotion and it’s an interesting way to communicate without words. I wanted to find a nicer way to have users select their color, but for now it’s tailored around the ease of working with CSS.
I have some error messages that show up when you don’t enter your color in the correct hexidecimal format.
I’m not sure how to make my server run forever - it seems to close whenever I log out of my digital ocean droplet.
UPDATE: I’ve learned how to run the node server forever. You use FOREVER. However, I don’t have this site up indefinitely and I’ll need to start it up via terminal if I need to demo it.
The important bit of the code is the part that is sending data between the client and the server. The server side is pretty simple. When a client is connected to the server and the server receives a message with a color, it emits the color to all the other clients.
On the client side, there’s more code. First, is the code that is getting the message from the user. And then sending it to the server. When the user hits enter, it checks if anything was entered and if the entry was actually a hex value. If the message is a valid hex color, I’ll change the background color of the chat as well as the text color so that it contrasts with the background. Then I’ll append the message to the chat body. Finally, I’ll emit the a message that includes the username, color, and text color to everyone as a comma separated string.