node.jsgosocket.io

How to connect to a Socket.IO NodeJS server using Go?


I'm trying to make a program in Go that connects to a NodeJS Socket.IO server. Here's a Minimal Reproductive Example:

A NodeJS Server using Socket.IO

const io = require("socket.io")(3000);

io.on("connection", (socket) => {
    console.log(`${socket.id} connected`);
});

A Go Client using go-socket.io

package main

import (
    "fmt"
    socketio "github.com/googollee/go-socket.io"
)

func main() {
    var url string = "http://localhost:3000"

    client, err := socketio.NewClient(url, nil)
    if err != nil { panic(err) }

    client.OnEvent("connection", func() {
        fmt.Println("connected as" + client.ID())
    })
}

With the above code, I get the following error when I try to run my code with go run main.go:

# command-line-arguments
./main.go:13:26: undefined: socketio.NewClient

Go version: go version go1.22.3 darwin/amd64

But on the documentation it says socketio.NewClient(...)

I have tried searching on Google about the related error but unfortunately I could not get a solution that worked for me.

Any help would be greatly appreciated, and thanks in advance.


Solution

  • Just as @JimB said, there’s no NewClient func in the version of package you are using. The NewClient func is available in the prerelease version v1.8.0-rc.1. To install it, you can simply run

    go get github.com/googollee/go-socket.io@v1.8.0-rc.1

    and go should do the things for you and I hope it worked for you.