Skip to content

A simple WebSocket server built with GoLang and Gin, integrated with RabbitMQ message broker for real-time messaging.

License

Notifications You must be signed in to change notification settings

ahmedMHasan/go-socket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-socket

A simple WebSocket server built with GoLang and Gin, integrated with RabbitMQ message broker for real-time messaging.

Features

  • WebSocket server sending periodic messages to clients.
  • Integration with RabbitMQ for broadcasting messages to connected clients.
  • Graceful shutdown mechanism.
  • Environment variable configuration.

Prerequisites

Getting Started

  1. Clone the repository:

    git clone https://github.com/ahmedMHasan/go-socket.git
  2. Navigate to the project directory:

    cd go-socket
  3. Set up your environment variables:

    export AMQP_URL=your_rabbitmq_url
  4. Build and run the server:

    go run main.go
  5. Access the WebSocket at ws://localhost:8080/ws from your WebSocket client.

Configuration

You can configure the following environment variables:

  • AMQP_URL: RabbitMQ server URL.

Usage

WebSocket Client Example

const socket = new WebSocket('ws://localhost:8080/ws');

socket.addEventListener('open', (event) => {
  console.log('Connected to the WebSocket server');
});

socket.addEventListener('message', (event) => {
  console.log('Received message:', event.data);
});

socket.addEventListener('close', (event) => {
  console.log('WebSocket connection closed:', event.reason);
});

socket.addEventListener('error', (event) => {
  console.error('WebSocket error:', event.error);
});

Graceful Shutdown

To gracefully stop the server, send a termination signal (e.g., SIGINT or SIGTERM) to the running process. The server will handle the shutdown and clean up resources.

To send a shutdown signal in Unix-like systems:

kill -SIGINT <process_id>

Replace <process_id> with the actual process ID of the running server.

References

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A simple WebSocket server built with GoLang and Gin, integrated with RabbitMQ message broker for real-time messaging.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages