“Websockets… Websockets… Websockets…”, I could do this all hour like Paul Allen shouted “Developers…Developers…Developers…” in some Microsoft Conference. I don’t remember the specific conference but you get the point. Websockets are the bread and butter of many Realtime systems today. They’re easy to implement in Node.js and mostly it is limited to web.
Phase - 1 | Brainstorm
I had searched google sometime ago on using websockets other than from browsers. Couldn’t find a non browser usage of websockets. I was also inspired by a project called Niltalk which uses Go’s websocket’s implementation with React but hey that too used the browser. So I went forth to make something using websockets and not TCP sockets. I wanted to write in Go since I knew Go and Go’s websocket implementation was ⚡️. I had never used websockets before writing Termchat and boy oh boy… They’re super cool.
Phase - 2 | Writing Code
Everytime I try to code in Go, I learn new things and use things in the Go STDlib that I had never used before. I for the First time used Goroutines and Structs here. The building part was a bit tricky since most of the blog posts or tutorials used webscokets where they sre usually used, in the Browser. Anyways the Gorilla/Websocket package is well documented and had better “reviews” than the Standard Go Websocket implementation. I used some of the code from their example and mashed this up.
The code uses goroutines for basic data transfer from client to Hub. There is a Hub and Client struct for the Data part. The User inputs the data and forwards it to the Hub and the gup broadcasts it to all other clients connected to the server. The `runner.go` file helps the clients connect to the server. It dials up to the websocket url and gives and input buffer for text entry. It was by accident when I actually found that this whole code actually works.
Here is a sample working 👇
The command part and connection is a simple
goroutine dialing the websocket and connecting to it, but the difference here is that it connects from a Terminal🤪.
Phase 3 - Making it Better
This is not the perfect implementation but it works, so sharing this 😅. I felt so proud that I hacked this up in the quarantine and learned a bit more of Go and websockets with this. Who knows, makng this better may help someone in some way. Some ways it could be used is,
- Secret message passing in Local network(Like in Programming labs in college😂)
- Organization Messaging
- Hackathons for messaging mentors and organizers
This phase is still going on and might improve Termchat’s functionality or design in the near future(I Hope).
What I learned from hacking this
Learning is a part of everyday now, so hacking this up helped me a learn a lot about
- Structs in Go
- Interfaces in Go
The chat in video is me talking to myself in 3 other aliases 🤪
Here is another nerdy websoket Meme 👇
If you find any typos or have any suggestions or questions, feel free to use the comments section