---
title: "Socket Client"
year: "2020"
motto: "Socket Client for Python Cluster"
description: "Socket client for a Python Cluster that converts WebSockets messages to TCP/IP or UDP understandable messages by the server."
stack: ["vuejs", "nestjs", "socketio", "nodejs", "typescript"]
website: "https://github.com/RolandoAndrade/socket-project"
url: "https://rolandoandrade.me/projects/socket-client/"
author: "Rolando Andrade"
---

# Socket Client

> Socket Client for Python Cluster

Socket client for a Python Cluster that converts WebSockets messages to TCP/IP or UDP understandable messages by the server.

Year: 2020. Stack: vuejs, nestjs, socketio, nodejs, typescript. Website: https://github.com/RolandoAndrade/socket-project

Repositories:

- [github](https://github.com/RolandoAndrade/socket-project)
- [gitlab](https://gitlab.com/RolandoAndrade/socket-project)

In our university networking course, we had a Python cluster that accepted TCP connections and answered through UDP. We wanted a web interface for it. There was one problem. Browsers cannot open raw TCP sockets. They only speak WebSockets and HTTP.

TCP and UDP are the two basic ways computers send data over a network. TCP keeps a connection open and delivers everything in order. UDP sends single packets with no connection at all. A WebSocket is a long lived connection a browser can open to a server.

So we built a bridge. It is a Node.js server that accepts WebSocket connections from the browser and turns them into TCP and UDP messages the Python cluster understands.

## Two halves of the bridge

The system has two parts: a Vue.js web client and a NestJS backend server.

The web client is a simple interface. Users enter their username and a UDP port number. Then they click buttons to send commands. They can say hello, ask for the message length, get the message, verify the checksum or disconnect.

The backend does the translating. When the browser sends a WebSocket message, the server turns it into a TCP command for the Python cluster. When the cluster answers through UDP, the server catches the data and pushes it back to the browser through the WebSocket.

![The browser only speaks WebSockets. The NestJS server sends commands over TCP and catches the message over UDP.](https://storage.rolandoandrade.me/blog/socket-client-bridge.webp)

## Logic apart from sockets

The project uses a layered architecture. The domain logic stays apart from the network code.

The messaging layer defines the operations: send a greeting, ask for the message length, get the message, check the checksum and disconnect. Each operation matches one command the Python cluster expects.

A repository does the actual socket work. It manages the TCP connection and creates UDP listeners when they are needed. When data arrives, it publishes events through an internal event bus.

The WebSocket gateway connects browsers to all of this. It receives commands from clients, passes them to the messaging service and sends the answers back. NestJS decorators make this short to write.

## Five commands over TCP

The Python cluster expects these text commands over TCP:

- `helloiam username` to log in
- `msglen` to get the message length
- `givememsg port` to receive the message on a UDP port
- `chkmsg hash` to verify the message is intact
- `bye` to disconnect

The server wraps these commands in socket operations. It keeps one TCP connection to the cluster open the whole time. For UDP, it opens a temporary listener only when the user asks for the message itself.

The cluster sends messages encoded in base64, a way of writing binary data as plain text. The server decodes them before passing them to the browser. That way binary data travels safely through the WebSocket.

## Events keep parts separate

An event bus connects the parts. When data arrives from the cluster, the socket repository publishes an event. The messaging service listens for these events and forwards them to connected browsers.

This keeps the code modular. The socket code knows nothing about WebSockets. The WebSocket code knows nothing about TCP. They only share events.

The browser uses events too. It listens for server answers and updates the screen. Success messages appear in blue cards. Retrieved quotes appear in purple. Errors appear in red.

## Catching the UDP message

Getting the message itself uses UDP, which makes it harder. It happens in four steps:

1. The browser picks a port.
2. The server opens a UDP socket on that port.
3. The server sends the TCP command that tells the cluster where to send the data.
4. The cluster sends the message through UDP. The server catches it, decodes the base64 and closes the UDP socket.

The decoded message then travels back to the browser through the WebSocket.

Running two protocols at once was the core challenge. TCP carries the conversation. UDP delivers the message. The server keeps both in step.

## Buttons that know the state

The Vue.js client uses Vuetify for styling. Buttons turn on and off based on the connection state. You cannot ask for a message before logging in. You cannot disconnect before connecting.

A history list shows every server answer. Each one is labeled as a success, message content or an error. Every action gets visible feedback.

The client talks to the server through Socket.IO. Socket.IO reconnects on its own if the connection drops. The connection status also appears in the history.

## Checking the message arrived

The protocol includes an integrity check. After getting a message, the user can verify it with the server. The client computes an MD5 hash of the message and sends it with the check command. A hash is a short fingerprint of the data.

If the fingerprints match, the message arrived intact. Data can get corrupted on the way. The checksum catches that before it matters.

## Firewalls, VPNs and timing

UDP needed special care. Unlike TCP, UDP keeps no connection. Each message stands alone. The server had to get the timing right: open the listener, tell the cluster where to send, wait for the data, clean up.

Windows firewalls caused problems during testing. They often block UDP packets from remote servers by default. We wrote down the firewall setup steps for Windows users.

The VPN added another layer. The Python cluster lived on a private network. When the VPN dropped, the errors were confusing. The server now retries connections on its own, but finding the cause the first time was hard.

## Two protocols, one job

This project showed me how different protocols work together. TCP gives reliable, ordered delivery for commands. UDP gives speed for moving data. Using both at once takes careful coordination.

Keeping protocol code apart from business logic made the code testable. The event bus kept the parts loosely connected. These patterns apply far beyond socket programming.

Real networks also have problems that theory does not cover: timeouts, retries and reconnections. Network code for production has to handle failure well.

* * *

If you are working with network protocols or need to bridge different communication systems, I would be happy to discuss approaches.

[Book a call with Rolando](https://rolandoandrade.me/pricing/)
