Skip to content

wsocket-io/sdk-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

wSocket Go SDK

Official Go SDK for wSocket — Realtime Pub/Sub over WebSockets.

Go Reference Go Version License: MIT

Installation

go get github.com/wsocket-io/sdk-go

Quick Start

package main

import (
    "fmt"
    wsocket "github.com/wsocket-io/sdk-go"
)

func main() {
    client := wsocket.NewClient("wss://node00.wsocket.online", "your-api-key")
    err := client.Connect()
    if err != nil {
        panic(err)
    }
    defer client.Disconnect()

    ch := client.Channel("chat:general")
    ch.Subscribe(func(data map[string]any, meta wsocket.MessageMeta) {
        fmt.Printf("[%s] %v\n", meta.Channel, data)
    })

    ch.Publish(map[string]any{"text": "Hello from Go!"})

    select {} // block forever
}

Features

  • Pub/Sub — Subscribe and publish to channels in real-time
  • Presence — Track who is online in a channel
  • History — Retrieve past messages
  • Connection Recovery — Automatic reconnection with message replay
  • Thread-safe — Safe for concurrent use

Presence

ch := client.Channel("chat:general")

ch.Presence().OnEnter(func(m wsocket.PresenceMember) {
    fmt.Println("joined:", m.ClientID)
})

ch.Presence().OnLeave(func(m wsocket.PresenceMember) {
    fmt.Println("left:", m.ClientID)
})

ch.Presence().Enter(map[string]any{"name": "Alice"})
members := ch.Presence().Get()

History

ch.OnHistory(func(result wsocket.HistoryResult) {
    for _, msg := range result.Messages {
        fmt.Printf("[%d] %v\n", msg.Timestamp, msg.Data)
    }
})

ch.History(wsocket.HistoryOptions{Limit: 50})

Requirements

  • Go >= 1.21
  • github.com/gorilla/websocket

Development

go test ./...

License

MIT

About

wSocket Go SDK — Realtime Pub/Sub client

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages