Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

GoSocketIO - A Socket.IO client and server library for Go, compatible with Socket.IO v3 and v4. Simplify real-time, bidirectional communication with customizable headers, event-based architecture, and built-in room support.

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.MIT
Notifications You must be signed in to change notification settings

erock530/gosf-socketio

Repository files navigation

GoSocketIO

GoDoc Go Report Card

GoSocketIO is a Socket.IO client and server library for Go, compatible with Socket.IO v3 and v4. This package allows you to build real-time, bidirectional communication applications with ease.

This library is based off the GoLang SocketIO Framework

This library was built with previous contributions by:

Features

  • Socket.IO v3 and v4 Compatible: Fully compatible with the latest versions of Socket.IO.
  • Extra Headers Support: Customize HTTP headers for WebSocket connections.
  • Event-based Architecture: Simplified event handling for connection, disconnection, and custom events.
  • Room Support: Easily broadcast messages to specific groups of clients.
  • Ping/Pong Mechanism: Built-in heartbeat mechanism to keep connections alive.

Installation

To install GoSocketIO, use go get:

go get github.com/erock530/gosf-socketio

Usage

Server Example

Here’s a simple WebSocket server using GoSocketIO:

package main

import (
    "fmt"
    "log"
    "net/http"

    "github.com/erock530/gosf-socketio"
    "github.com/erock530/gosf-socketio/transport"
)

func main() {
    server := gosocketio.NewServer(transport.GetDefaultWebsocketTransport())

    server.On(gosocketio.OnConnection, func(c *gosocketio.Channel) {
        fmt.Println("New client connected:", c.Id())
        c.Join("chat")
    })

    server.On(gosocketio.OnDisconnection, func(c *gosocketio.Channel) {
        fmt.Println("Client disconnected:", c.Id())
    })

    server.On("chat message", func(c *gosocketio.Channel, message string) {
        fmt.Printf("Received message from %s: %s\n", c.Id(), message)
        server.BroadcastTo("chat", "chat message", message)
    })

    http.Handle("/socket.io/", server)

    addr := "localhost:8080"
    fmt.Printf("Serving at %s...\n", addr)
    log.Fatal(http.ListenAndServe(addr, nil))
}

For more details, see the Server Example

Go Client Example

Here’s how you can create a Go client that connects to the server:

package main

import (
    "fmt"
    "log"
    "time"

    "github.com/erock530/gosf-socketio"
    "github.com/erock530/gosf-socketio/transport"
)

func main() {
    wsTransport := transport.GetDefaultWebsocketTransport()
    options := &gosocketio.Options{
        ExtraHeaders: []gosocketio.ExtraHeaders{
            {Key: "Content-Type", Value: "application/json"},
            {Key: "Authorization", Value: "Bearer YOUR_ACCESS_TOKEN"},
        },
        PingTimeout:  60 * time.Second,
        PingInterval: 30 * time.Second,
    }

    client, err := gosocketio.Dial("ws://localhost:8080/socket.io/?EIO=3&transport=websocket", wsTransport, options)
    if err != nil {
        log.Fatalf("Failed to connect to server: %v", err)
    }
    defer client.Close()

    client.On(gosocketio.OnConnection, func(c *gosocketio.Channel) {
        fmt.Println("Connected to server")
    })

    client.On(gosocketio.OnDisconnection, func(c *gosocketio.Channel) {
        fmt.Println("Disconnected from server")
    })

    client.On("chat message", func(c *gosocketio.Channel, message string) {
        fmt.Printf("Received message: %s\n", message)
    })

    err = client.Emit("chat message", "Hello, Server!")
    if err != nil {
        log.Printf("Failed to send message: %v", err)
    }

    select {}
}

For more details, see the Go Client Example

JavaScript Client Example

Here’s a simple JavaScript client using Socket.IO:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Socket.IO Client</title>
</head>
<body>
    <h1>WebSocket Chat</h1>
    <div id="chat">
        <ul id="messages"></ul>
    </div>
    <form id="form" action="">
        <input id="input" autocomplete="off" /><button>Send</button>
    </form>
    <script src="https://cdn.socket.io/4.0.1/socket.io.min.js"></script>
    <script>
        var socket = io('http://localhost:8080');

        socket.on('connect', function() {
            console.log('Connected to server');
        });

        socket.on('disconnect', function() {
            console.log('Disconnected from server');
        });

        socket.on('chat message', function(msg) {
            var item = document.createElement('li');
            item.textContent = msg;
            document.getElementById('messages').appendChild(item);
        });

        document.getElementById('form').addEventListener('submit', function(e) {
            e.preventDefault();
            var input = document.getElementById('input');
            socket.emit('chat message', input.value);
            input.value = '';
        });
    </script>
</body>
</html>

For more details, see the JavaScript Client Example

Contributing

We welcome contributions! Please check out the Contributing Guide for more details.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

GoSocketIO - A Socket.IO client and server library for Go, compatible with Socket.IO v3 and v4. Simplify real-time, bidirectional communication with customizable headers, event-based architecture, and built-in room support.

Resources

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.MIT

Stars

Watchers

Forks

Packages

No packages published

Languages