A simple WebSocket server built with GoLang and Gin, integrated with RabbitMQ message broker for real-time messaging.
- WebSocket server sending periodic messages to clients.
- Integration with RabbitMQ for broadcasting messages to connected clients.
- Graceful shutdown mechanism.
- Environment variable configuration.
- Go 1.15 or higher
- RabbitMQ server
- Gin and Gorilla WebSocket dependencies
-
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.
You can configure the following environment variables:
AMQP_URL
: RabbitMQ server URL.
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);
});
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.
-
Building a Production-Grade WebSocket for Notifications with Golang and Gin by [Abhishek Ranjan]
This Medium article provides a detailed guide on building a WebSocket server for notifications using Golang and Gin. It served as a valuable reference for implementing real-time messaging in this project.
This project is licensed under the MIT License - see the LICENSE file for details.