go-socket

command module
v0.0.0-...-4d2f289 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2023 License: MIT Imports: 8 Imported by: 0

README

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.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL