Skip to content

abelrgr/socket-server

Repository files navigation

socket-server

A NestJS multi-protocol echo server that exposes HTTP, native WebSocket, Socket.IO, and embedded MQTT endpoints from a single application.

NestJS TypeScript Socket.IO WebSocket MQTT Jest

Features

  • HTTP root endpoint that returns Hello World!
  • Native WebSocket echo server on the same HTTP port
  • Socket.IO echo gateway on a dedicated configurable port
  • Embedded MQTT broker powered by aedes
  • MQTT support over TCP and WebSocket (/mqtt)
  • Connection logging and welcome/response payloads for each protocol
  • Global configuration support through @nestjs/config

Technologies Used

  • NestJS
  • TypeScript
  • @nestjs/config
  • socket.io
  • ws
  • mqtt
  • aedes
  • Jest
  • Supertest

Quick Start

Prerequisites

  • Node.js
  • npm

No external MQTT broker is required. The MQTT broker is embedded in the application.

Installation

npm install

Environment Variables

The application works with defaults, but these optional variables are read at runtime:

Variable Default Purpose
PORT 4500 HTTP server port and native WebSocket entry point
SIO_PORT 4502 Socket.IO server port
MQTT_TCP_PORT 1883 MQTT broker TCP port
MQTT_REQUEST_TOPIC test/request Logged at startup only
MQTT_RESPONSE_TOPIC test/response Logged at startup only

[TODO: complete this section if you want to document deployment-specific variables or client-side configuration.]

Running the application

npm run dev

The application starts the HTTP server first, then attaches the native WebSocket server and the embedded MQTT broker automatically.

Development

Use the following commands during development:

npm run dev
npm run start:debug
npm run format
npm run lint

Production

npm run build
npm run start:prod

Usage

  • Main HTTP endpoint: GET /
  • Native WebSocket: ws://localhost:4500
  • Socket.IO: http://localhost:4502
  • MQTT over TCP: mqtt://localhost:1883
  • MQTT over WebSocket: ws://localhost:4500/mqtt
  • The default HTTP response is Hello World!
  • Socket.IO listens for the message event and returns an response payload
  • Native WebSocket clients receive a welcome message on connect and an echoed response for each message
  • MQTT clients publish to any topic and receive an echoed message under echo/<topic>

Basic request example:

curl http://localhost:4500/

Expected response:

Hello World!

Swagger/OpenAPI documentation is not configured in this repository yet, so there is no /api docs route at the moment.

Project Structure

.
├── src
│   ├── app.controller.ts
│   ├── app.module.ts
│   ├── app.service.ts
│   ├── main.ts
│   ├── mqtt
│   │   ├── mqtt.module.ts
│   │   └── mqtt.service.ts
│   ├── socketio
│   │   ├── socketio.gateway.ts
│   │   └── socketio.module.ts
│   └── websocket
│       ├── websocket.module.ts
│       └── websocket.service.ts
├── test
│   └── app.e2e-spec.ts
├── package.json
└── docker-compose.yml

API Documentation

Swagger/OpenAPI is not set up in the current codebase.

[TODO: complete this section if you add @nestjs/swagger and expose a documentation route.]

Available Scripts

  • npm run build - Compile the NestJS application into dist/
  • npm run format - Format TypeScript sources with Prettier
  • npm run start - Start the application in normal mode
  • npm run dev - Start the application in watch mode
  • npm run start:debug - Start the application in watch mode with the debugger enabled
  • npm run start:prod - Run the compiled production build from dist/main
  • npm run lint - Lint and auto-fix TypeScript files with ESLint
  • npm run test - Run the unit test suite with Jest
  • npm run test:watch - Run Jest in watch mode
  • npm run test:cov - Generate a coverage report
  • npm run test:debug - Run Jest under the Node.js inspector
  • npm run test:e2e - Run end-to-end tests

Running Tests

Run the automated tests with:

npm run test

Other useful test commands:

npm run test:watch
npm run test:cov
npm run test:e2e

The current test coverage is minimal and focuses on the root controller plus a basic end-to-end check for GET /.

How to Contribute

  1. Fork the repository.
  2. Create a feature branch.
  3. Make your changes.
  4. Run npm run lint and the relevant tests.
  5. Open a pull request with a clear description of the change.

License

This project is currently licensed as UNLICENSED.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors