This repository contains example implementations of a Socket.IO v5 client written in Go, along with a mock server for testing.
main.go- Go client example demonstrating Socket.IO v5 featuresmock-server/- NestJS-based mock Socket.IO server for testing
- Socket.IO v5 client implementation using go.socket.io
- Custom high-speed JSON parser integration using go.socket.io-parser.jsoniter
- WebSocket transport configuration
- Event handling with acknowledgments and timeouts
- Context-based lifecycle management
- Structured logging with Zap
- Built with NestJS and Socket.IO
- Provides test endpoints for all client features
- 100% test coverage
- Docker support for easy deployment
- Go 1.18 or higher
- Node.js v20 or higher (for mock server)
- Docker and Docker Compose (optional)
# Clone the repository
git clone https://github.com/maldikhan/go.socket.io-examples.git
cd go.socket.io-examples
# Start both server and client
docker-compose up
# Or run them separately
docker-compose up mock-server # Start server only
go run main.go # Run client locallycd mock-server
yarn install
yarn start:devThe server will start on port 3300.
# From the root directory
go mod download
go run main.goThe mock server provides the following test endpoints:
- Connection Test - Validates connection and authentication
- Simple Echo (
hi) - Basic request-response pattern - Async Operations (
delay) - Tests timeouts and async handling - Load Testing (
load) - High-frequency message handling - Data Validation (
get_square,get_sum) - Complex data types and validation
// Custom client with high-performance parser
client, err := socketio_v5_client.NewClient(
socketio_v5_client.WithEngineIOClient(eioClient),
socketio_v5_client.WithLogger(logger),
socketio_v5_client.WithParser(socketio_v5_parser_default.NewParser(
socketio_v5_parser_default.WithPayloadParser(
socketio_v5_parser_default_jsoniter.NewPayloadParser()
),
)),
)// Simple emit with acknowledgment
client.Emit("hi", emit.WithAck(func(response string) {
fmt.Println("Server said:", response)
}))
// Emit with timeout
client.Emit("delay", 1000,
emit.WithAck(func(response string) {...}),
emit.WithTimeout(500*time.Millisecond, func() {...})
)
// Handle server events
client.On("result", func(operation string, result int) {
fmt.Printf("%s = %d\n", operation, result)
})By default, the client connects to http://127.0.0.1:3300. You can change this using:
- Environment Variable:
export SOCKETIO_SERVER_URL="http://your-server:port/socket.io/"
go run main.go- Code Modification:
serverURL := "http://your-server:port/socket.io/"
eioClient, err := engineio_v4_client.NewClient(
engineio_v4_client.WithRawURL(serverURL),
// ... other options
)The mock server runs on port 3300 by default. See mock-server/README.md for detailed configuration options.
# Test the mock server
cd mock-server
yarn test # Unit tests
yarn test:e2e # End-to-end tests
yarn test:cov # Coverage report# Build server image
docker build -t socketio-mock-server ./mock-server
# Or use docker-compose
docker-compose build- go.socket.io - Socket.IO v5 client library for Go
- go.socket.io-parser.jsoniter - High-performance JSON parser
- zap - Structured logging library
- NestJS - Node.js framework
- Socket.IO - Real-time bidirectional communication
- Jest - Testing framework
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.