Skip to content

Socket.IO Client for Go Documentation

Welcome to the complete documentation for the Socket.IO client for Go. This library provides a full implementation of the Socket.IO v4 protocol.

πŸš€ Quick Start

First time using this library? Start here:

πŸ“˜ Getting Started Guide - Install and create your first connection in 5 minutes

client := socketio.New("ws://localhost:3000")
client.OnConnect(func() {
  client.Emit("hello", "world")
})

πŸ“š Guides

Core Concepts

  • Event System - Learn how to emit and listen to events, handle acknowledgments, and work with binary events

First Steps

  1. Installation - How to install the library
  2. Your First Connection - Connect and emit your first event
  3. Core Concepts - Socket, events, namespaces, and lifecycle
  4. Basic Configuration - Common configuration options

πŸ”§ API Reference

Complete documentation for all public APIs:

Core

  • Socket - Main Socket.IO client
  • Creation and configuration
  • Connection methods
  • Event emission and reception
  • State management

  • Namespace - Multiplexed communication channels

  • Namespace retrieval and usage
  • Events per namespace
  • Context isolation

  • Options - Client configuration

  • Authentication
  • Timeouts and reconnection
  • Custom headers

🎯 Advanced Usage

Advanced patterns and techniques for complex use cases:

  • Advanced Namespaces
  • Namespace architecture
  • Implementation patterns
  • Namespace manager
  • Dynamic namespaces

  • Acknowledgments

  • Request-response pattern
  • Timeout handling
  • Multiple parallel requests
  • Promise-like API

  • Binary Events

  • Efficient data transmission
  • File upload/download
  • Audio/video streaming
  • Compression and checksums

  • Automatic Reconnection

  • Reconnection configuration
  • Exponential backoff
  • State re-synchronization
  • Offline queue

πŸ“– Examples by Use Case

By Functionality

Use Case Example Documentation
Basic connection examples/basic/ Getting Started
JWT authentication examples/authentication/ Options API
Real-time chat examples/chat/ Events
Multiple namespaces examples/namespaces/ Advanced Namespaces
Request-response examples/acknowledgments/ Acknowledgments
File transfer examples/binary/ Binary Events
Resilient reconnection examples/reconnection/ Reconnection
Rooms and broadcasting examples/rooms/ Namespaces

By Level

Basic Level: - Simple connection - Emit and receive events - Disconnection handling

Intermediate Level: - Authentication - Acknowledgments - Multiple namespaces

Advanced Level: - Binary streaming - Custom reconnection - Offline queue

πŸŽ“ Recipes

Quick solutions for common problems:

Connection and Authentication

// Connect with JWT token
client := socketio.New("ws://api.example.com", socketio.Options{
  Auth: map[string]interface{}{
    "token": "eyJhbGciOiJIUzI1NiIs...",
  },
})

Error Handling

// Handle all possible errors
client.OnError(func(err error) {
  log.Println("Error:", err)
})

client.OnReconnectError(func(err error) {
  log.Println("Reconnection error:", err)
})

Connection State

// Check state before emitting
if client.IsConnected() {
  client.Emit("event", data)
} else {
  log.Println("Not connected, queuing...")
}

Request-Response

// Request data with timeout
client.EmitWithAck("get-data", func(response ...interface{}) {
  if response == nil {
    log.Println("Timeout")
    return
  }
  data := response[0]
  processData(data)
}, "param")

Multiple Namespaces

// Organize by functionality
chat := client.Of("/chat")
chat.On("message", handleChatMessage)

notifications := client.Of("/notifications")
notifications.On("alert", handleAlert)

πŸ” Quick Reference

By Method

Method Description Docs
New() Create client Socket API
On() Listen to events Socket API
Emit() Emit events Socket API
EmitWithAck() Emit with response Socket API
Of() Get namespace Socket API
OnConnect() Connection handler Socket API
OnDisconnect() Disconnection handler Socket API
Close() Close connection Socket API
IsConnected() Check state Socket API

By Event

Event When it executes Docs
OnConnect On successful connection Events
OnDisconnect On disconnection Events
OnError On error occurrence Events
OnReconnectAttempt Before each reconnection attempt Events
OnReconnect On successful reconnection Events
OnReconnectError On failed attempt Events
OnReconnectFailed On all attempts failed Events

By Option

Option Purpose Docs
Auth Authentication Options
Headers HTTP headers Options
ReconnectAttempts Maximum attempts Options
ReconnectDelay Initial delay Options
ReconnectDelayMax Maximum delay Options
Timeout Connection timeout Options
AckTimeout ACK timeout Options

πŸ› οΈ Troubleshooting

Common Issues

Not connecting: 1. Verify URL (should be ws:// or wss://) 2. Verify server is running 3. Check error logs with OnError()

Events not received: 1. Verify handler is registered before connection 2. Verify event name (case-sensitive) 3. Verify you're on the correct namespace

Acknowledgment timeout: 1. Increase AckTimeout in Options 2. Verify server responds correctly 3. Implement retry logic

Reconnection fails: 1. Verify reconnection configuration 2. Check OnReconnectError for reason 3. Implement state re-synchronization

Debugging

// Enable all event handlers
client.OnConnect(func() {
  log.Println("[DEBUG] Connected")
})

client.OnDisconnect(func(reason string) {
  log.Printf("[DEBUG] Disconnected: %s\n", reason)
})

client.OnError(func(err error) {
  log.Printf("[DEBUG] Error: %v\n", err)
})

client.OnReconnectAttempt(func(attempt int) {
  log.Printf("[DEBUG] Reconnecting... (#%d)\n", attempt)
})

// Log all events
client.On("*", func(data ...interface{}) {
  log.Printf("[DEBUG] Event received: %v\n", data)
})

🀝 Compatibility

Socket.IO Server Engine.IO Status
v4.x v4 βœ… Complete
v3.x v4 βœ… Complete
v2.x v3 ⚠️ Partial
v1.x v2 ❌ Not supported

πŸ“Š Features

  • βœ… Socket.IO v4 Protocol
  • βœ… Engine.IO v4 with HTTP long-polling and WebSocket
  • βœ… Namespaces for multiplexing
  • βœ… Event acknowledgments with timeout
  • βœ… Binary event transmission
  • βœ… Authentication in handshake
  • βœ… Automatic reconnection with exponential backoff
  • βœ… Thread-safe with Go concurrency primitives
  • βœ… Zero data races (validated with Go Race Detector)
  • βœ… High performance (10,000+ events/second)

πŸ“ Release Notes

v1.0.0 (Current) - Complete Socket.IO v4 implementation - Namespace and acknowledgment support - Automatic reconnection - Thread-safe and production-ready

πŸ“„ License

MIT License - See LICENSE


Have questions? Open an issue on GitHub

Found a bug? Report the issue

Want to contribute? Read the Contribution Guide