With this lab project I wanted to explore of the usage of WebSockets across both
the client and server side. The basic idea was to track cursor movements on the
client side, send the data to the server, then the server would then broadcast
the cursor movements to all connected clients. The result is a ghostly cursor
that follows the movements other users make on the page.
Architecture
With websockets, the client initiates a connection with the server. The server
then broadcasts messages to all connected clients. The server also maintains a
connection to a database to store records of users that have connected.
In this way its possible to build multiple http routes for various clients data
streams, for example an Active User feed which would broadcast the most recent
users and if they are currently active.
Implementation
Building the server in Go was a fun and interesting challenge. Since it was
dedicated to the handling of events coming in from the client it made much more
sense to use Go then Node.js. The server is built using the gorilla/websocket
package which provides a simple API for building a WebSocket servers and the
simplicity of Go modules made separating the logic between the server, database
and manager.
With the server built, the client was built using Vite, React and the
useWebSocket hook from react-use-websocket. The hook provides a simple API
for connecting to a WebSocket server and sending and receiving messages.
The important part was setting up a system between the client and server where
messages would have a type and payload. This allowed for the server to
differentiate between different types of messages and act accordingly. For the
time being this is just used to update the position, but can be easily expanded
to include other types of messages.
Next Steps
Right now I'm pretty happy with the current state, but have lots of ideas for
what could be done with the technology. Some ideas include:
Force Driven Data Visualization -> Users can push and pull data points on
a map.
Maze Game -> Users can navigate a maze together.
Real-time Chat Room -> Users can chat with each other in real-time, but
only when they are close to each other.
I'm excited to see where this project goes and what other ideas I can come up
with.