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
-
Clone the repository:
git clone https://github.com/ahmedMHasan/go-socket.git
-
Navigate to the project directory:
cd go-socket
-
Set up your environment variables:
export AMQP_URL=your_rabbitmq_url
-
Build and run the server:
go run main.go
-
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.