Skip to content

WebSocket

Manages a WebSocket connection to VRChat's pipeline server for real-time event streaming.

Setup

go
import (
    "context"
    "github.com/kqnade/vrcgo/vrcws"
    "github.com/kqnade/vrcgo/shared"
)

// Requires an already-authenticated REST client
ws, err := vrcws.New(context.Background(), client)
if err != nil {
    log.Fatal(err)
}
defer ws.Close()

// Register handlers, then block
ws.Wait()

Event Handlers

On(eventType, handler)

Register a handler for any event type. Use "*" as a wildcard to catch all events.

go
ws.On("friend-online", func(e shared.Event) {
    fmt.Printf("Raw event: %s\n", e.Content)
})

ws.On("*", func(e shared.Event) {
    fmt.Printf("Any event: %s\n", e.Type)
})

OnNotification(handler)

Receive notification events.

go
ws.OnNotification(func(n shared.NotificationEvent) {
    fmt.Printf("Notification [%s]: %s\n", n.Type, n.Message)
})

OnFriendOnline(handler)

Called when a friend comes online.

go
ws.OnFriendOnline(func(e shared.FriendOnlineEvent) {
    fmt.Printf("%s is now online at %s\n", e.UserID, e.Location)
})

OnFriendOffline(handler)

Called when a friend goes offline.

go
ws.OnFriendOffline(func(e shared.FriendOfflineEvent) {
    fmt.Printf("%s went offline\n", e.UserID)
})

OnFriendLocation(handler)

Called when a friend changes their location.

go
ws.OnFriendLocation(func(e shared.FriendLocationEvent) {
    fmt.Printf("%s moved to %s\n", e.UserID, e.Location)
})

OnFriendActive(handler)

Called when a friend becomes active (e.g., launches the game).

go
ws.OnFriendActive(func(e shared.FriendActiveEvent) {
    fmt.Printf("%s is now active\n", e.UserID)
})

OnFriendAdd(handler) / OnFriendDelete(handler)

Called when a friend is added or removed.

go
ws.OnFriendAdd(func(e shared.FriendAddEvent) {
    fmt.Printf("Now friends with %s\n", e.UserID)
})

ws.OnFriendDelete(func(e shared.FriendDeleteEvent) {
    fmt.Printf("No longer friends with %s\n", e.UserID)
})

OnUserUpdate(handler)

Called when your own user data is updated.

go
ws.OnUserUpdate(func(e shared.UserUpdateEvent) {
    fmt.Println("Your user profile was updated")
})

Supported Events

Event TypeHandlerDescription
notificationOnNotificationNotification received
friend-onlineOnFriendOnlineFriend came online
friend-offlineOnFriendOfflineFriend went offline
friend-activeOnFriendActiveFriend became active
friend-locationOnFriendLocationFriend changed location
friend-addOnFriendAddFriend added
friend-deleteOnFriendDeleteFriend removed
friend-updateFriend info updated
user-updateOnUserUpdateOwn user info updated
user-locationOwn location changed
notification-v2Notification V2
notification-v2-updateNotification V2 updated
notification-v2-deleteNotification V2 deleted
group-joinedJoined a group
group-leftLeft a group
group-member-updatedGroup member updated
group-role-updatedGroup role updated

Auto-Reconnect

The WebSocket client automatically reconnects on disconnection using exponential backoff.

  • Initial retry delay: 5 seconds
  • Maximum delay: 60 seconds
  • Backoff multiplier:
go
// Cancel the context to stop reconnecting
ctx, cancel := context.WithCancel(context.Background())
ws, _ := vrcws.New(ctx, client)

cancel() // stop reconnecting and close
ws.Wait()

Other Methods

Close()

Close the WebSocket connection.

go
ws.Close()

Wait()

Block until the connection is closed. Call from your main goroutine to keep the program running.

go
ws.Wait()

Released under the Apache-2.0 License.