Skip to content

mdaxf/iac-signalr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IAC SignalR Server

A high-performance SignalR server implementation in Go for real-time communication and messaging.

Features

  • Real-time Messaging: WebSocket and Server-Sent Events (SSE) support
  • Group Management: Subscribe and broadcast messages to client groups
  • Structured Logging: Comprehensive logging with multiple adapters (console, file, MongoDB, etc.)
  • Health Monitoring: Built-in health check endpoint and heartbeat mechanism
  • CORS Support: Configurable Cross-Origin Resource Sharing
  • Secure Authentication: API key-based authentication with constant-time comparison
  • Docker Support: Containerized deployment with multi-stage builds

Quick Start

Prerequisites

  • Go 1.21.4 or higher
  • Docker (optional, for containerized deployment)

Installation

# Clone the repository
git clone https://github.com/mdaxf/iac-signalr.git
cd iac-signalr

# Install dependencies
go mod download

# Build the application
go build -o iac-signalr

Configuration

Create a signalrconfig.json file (or copy from signalrconfig.json.example):

{
    "address": "0.0.0.0:8222",
    "clients": "http://127.0.0.1:8080,https://127.0.0.1:8080",
    "insecureSkipVerify": false,
    "appserver": {
        "url": "http://127.0.0.1:8080",
        "apikey": "your-secret-api-key"
    },
    "log": {
        "adapter": "console",
        "level": "info"
    }
}

Important: Never commit your actual configuration files with sensitive data. Use environment variables for production deployments.

Environment Variables

The server supports the following environment variables (which override configuration file values):

  • SIGNALR_CONFIG: Path to configuration file (default: signalrconfig.json)
  • SIGNALR_ADDRESS: Server listen address (e.g., 0.0.0.0:8222)
  • SIGNALR_CLIENTS: Allowed client origins (comma-separated)
  • SIGNALR_API_KEY: API key for authentication (recommended over config file)
  • SIGNALR_INSECURE_SKIP_VERIFY: Set to true to disable origin verification (not recommended for production)

Running the Server

# Using the binary
./iac-signalr

# Or with Go
go run .

# With environment variables
SIGNALR_API_KEY=your-secret-key SIGNALR_ADDRESS=0.0.0.0:8222 ./iac-signalr

Docker Deployment

# Build the Docker image
docker build -t iac-signalr:latest .

# Run the container
docker run -d \
  -p 8222:8222 \
  -e SIGNALR_API_KEY=your-secret-key \
  -e SIGNALR_CLIENTS=http://your-frontend.com \
  --name iac-signalr \
  iac-signalr:latest

API Documentation

Health Check Endpoint

Endpoint: GET /health

Headers:

  • Authorization: apikey <your-api-key>

Response:

{
    "Node": {
        "Name": "iac-signalr",
        "AppID": "uuid",
        "Host": "hostname",
        "IPAddress": "192.168.1.1",
        "OS": "linux",
        "Version": "1.0.0",
        "Status": "Running"
    },
    "Result": {
        "status": "healthy"
    },
    "timestamp": "2025-01-15T12:00:00Z"
}

SignalR Hub Methods

The IAC Message Bus hub supports the following methods:

Subscribe

Subscribe to a topic for receiving messages.

connection.invoke("Subscribe", topic, connectionId);

Send

Send a message to all subscribers of a topic.

connection.invoke("Send", topic, message, connectionId);

SendToBackEnd

Send a message specifically to backend listeners.

connection.invoke("SendToBackEnd", topic, message, connectionId);

Broadcast

Broadcast a message to all connected clients.

connection.invoke("Broadcast", message);

Logging

The server supports multiple logging adapters:

  • Console: Standard output logging
  • File: File-based logging with rotation
  • MultiFile: Separate log files per level
  • DocumentDB: MongoDB logging
  • SMTP: Email notifications for critical errors

Configure logging in signalrconfig.json:

{
    "log": {
        "adapter": "console",
        "level": "info"
    }
}

Log levels (from most to least severe):

  • emergency
  • alert
  • critical
  • error
  • warning
  • notice
  • info
  • debug

Security Best Practices

  1. Always use environment variables for sensitive data (API keys, connection strings)
  2. Enable HTTPS in production environments
  3. Set insecureSkipVerify: false in production
  4. Restrict CORS origins to only trusted domains
  5. Use strong API keys (at least 32 characters, random)
  6. Keep dependencies updated regularly

Development

Running Tests

# Run all tests
go test ./...

# Run tests with coverage
go test -cover ./...

# Run specific package tests
go test ./signalr/...

Building for Different Platforms

# Linux
GOOS=linux GOARCH=amd64 go build -o iac-signalr-linux

# Windows
GOOS=windows GOARCH=amd64 go build -o iac-signalr.exe

# macOS
GOOS=darwin GOARCH=amd64 go build -o iac-signalr-darwin

Architecture

iac-signalr/
├── signalr/          # Core SignalR protocol implementation
├── middleware/       # HTTP middleware (CORS, logging)
├── logger/           # Logging infrastructure
├── router/           # HTTP router implementations
├── public/           # Static file serving
├── client/           # Client implementations
├── chatsample/       # Example chat application
└── server.go         # Main server entry point

Troubleshooting

Connection Issues

  1. Check firewall rules allow traffic on the configured port
  2. Verify CORS settings match your client origin
  3. Ensure WebSocket support is enabled in your proxy/load balancer

Authentication Failures

  1. Verify API key is correctly set in environment or config
  2. Check Authorization header format: apikey <your-key>
  3. Ensure no whitespace or special characters in the API key

Performance Issues

  1. Monitor log level - debug logging can impact performance
  2. Check connection limits and system resources
  3. Consider using load balancing for high traffic

Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes with clear messages
  4. Add tests for new functionality
  5. Submit a pull request

License

Licensed under the Apache License, Version 2.0. See LICENSE.md for details.

Support

For issues and questions:

  • Open an issue on GitHub
  • Check existing documentation and examples
  • Review the chat sample application for implementation guidance

Changelog

Version 1.0.0 (2025-01-15)

Security Improvements

  • Replaced hardcoded API keys with environment variable support
  • Implemented constant-time string comparison for authentication
  • Made InsecureSkipVerify configurable (defaults to false)

Logging Enhancements

  • Replaced verbose header logging with structured logging
  • Implemented SignalRLogAdapter for go-kit compatibility
  • Removed excessive debug output from production code

Code Quality

  • Replaced deprecated ioutil with io and os packages
  • Fixed potential nil pointer dereference in GetHostandIPAddress
  • Improved error handling in main() and server initialization
  • Added comprehensive .gitignore

Documentation

  • Added complete README with examples
  • Documented all configuration options
  • Included security best practices
  • Added troubleshooting guide

About

The signalr server

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors