socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Support for gofiber.

Open itzTheMeow opened this issue 1 year ago • 13 comments

Is there a working example for using this library with gofiber?

I tried this:

import (
	"github.com/gofiber/adaptor/v2"
	"github.com/zishang520/socket.io/socket"
)

// ...
io := socket.NewServer(nil, nil)
App.Use("/socket.io", adaptor.HTTPHandler(io.ServeHandler(nil)))

and socket.io can not connect to the websocket. (it resorts to using polling)

itzTheMeow avatar Apr 15 '23 03:04 itzTheMeow

I have received your question and will give you a relevant answer after follow-up research.

zishang520 avatar Apr 15 '23 14:04 zishang520

Thanks for your patience, as Fiber is built on top of Fasthttp, Fiber is not compatible with net/http interface, please look forward to follow-up support.

zishang520 avatar Apr 15 '23 15:04 zishang520

alright, thanks

itzTheMeow avatar Apr 16 '23 02:04 itzTheMeow

Maybe need a abstract interface. My project is based Hertz framework. It costs a lot to transfer Hertz context to Standard Net request and writer.

Dorbmon avatar May 22 '23 07:05 Dorbmon

@zishang520 any status on this?

itzTheMeow avatar Aug 03 '23 16:08 itzTheMeow

@zishang520 any status on this?

At present, the work based on fasthttp is basically completed, and it is still being debugged. There are still some other work in progress: redis-based adapter.

zishang520 avatar Aug 04 '23 00:08 zishang520

sounds good!

itzTheMeow avatar Aug 08 '23 12:08 itzTheMeow

any status on this?

itzTheMeow avatar Oct 24 '23 14:10 itzTheMeow

any status on this?

It may take some time. I am updating engine.io to 6.5.3 and some work has not been completed yet.

zishang520 avatar Oct 25 '23 10:10 zishang520

another status?

itzTheMeow avatar Feb 20 '24 04:02 itzTheMeow

another status?

I'm really sorry. The project is progressing slowly. I've been experiencing some financial stress recently. I have been without income for more than half a year, and my baby is about to be born. For me, it's double pressure. But don't be discouraged, I will continue to complete the development when I have enough time and financial resources. Thank you all for your anticipation and understanding. Finally say sorry again.

zishang520 avatar Feb 21 '24 02:02 zishang520

That's perfectly fine, take any time you need to sort yourself out. Life comes first.

itzTheMeow avatar Feb 22 '24 13:02 itzTheMeow

is there a way we can support this implementation? gofiber is quite popular that it won't be supported

danielAsaboro avatar Apr 06 '24 12:04 danielAsaboro

Sorry, due to technical issues, I cannot solve this problem: https://github.com/valyala/fasthttp/issues/965. Currently, this requirement will be updated after fasthttp has a next step plan.

Currently, the submitted engine.io-server-go-fasthttp project and socket.io-server-go-fasthttp project cannot be used normally

zishang520 avatar May 31 '24 08:05 zishang520

~New progress: Currently it can only be implemented based on the Websocket transport interface, and the Polling transport interface will be abandoned.~

New research finds that it is now available, However, I also made a fasthttp related adaptation, but it only supports websocket: socket.io-server-go-fasthttp

zishang520 avatar Jul 11 '24 06:07 zishang520

Fasthttp has been updated not long ago (https://github.com/valyala/fasthttp/pull/1525/commits/9c12719b5fe70391ac9babc759478786ab96dff3), and it was found to work properly in the test case:

package main

import (
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/adaptor"
	"github.com/zishang520/engine.io/v2/log"
	"github.com/zishang520/engine.io/v2/types"
	"github.com/zishang520/socket.io/v2/socket"
)

func main() {
	log.DEBUG = true
	c := socket.DefaultServerOptions()
	c.SetServeClient(true)
	// c.SetConnectionStateRecovery(&socket.ConnectionStateRecovery{})
	// c.SetAllowEIO3(true)
	c.SetPingInterval(300 * time.Millisecond)
	c.SetPingTimeout(200 * time.Millisecond)
	c.SetMaxHttpBufferSize(1000000)
	c.SetConnectTimeout(1000 * time.Millisecond)
	c.SetCors(&types.Cors{
		Origin:      "*",
		Credentials: true,
	})
	socketio := socket.NewServer(nil, nil)
	socketio.On("connection", func(clients ...interface{}) {
		client := clients[0].(*socket.Socket)

		client.On("message", func(args ...interface{}) {
			client.Emit("message-back", args...)
		})
		client.Emit("auth", client.Handshake().Auth)

		client.On("message-with-ack", func(args ...interface{}) {
			ack := args[len(args)-1].(func([]any, error))
			ack(args[:len(args)-1], nil)
		})
	})

	socketio.Of("/custom", nil).On("connection", func(clients ...interface{}) {
		client := clients[0].(*socket.Socket)
		client.Emit("auth", client.Handshake().Auth)
	})

	app := fiber.New()

	// app.Put("/socket.io", adaptor.HTTPHandler(socketio.ServeHandler(c))) // test
	app.Get("/socket.io", adaptor.HTTPHandler(socketio.ServeHandler(c)))
	app.Post("/socket.io", adaptor.HTTPHandler(socketio.ServeHandler(c)))

	go app.Listen(":3000")

	exit := make(chan struct{})
	SignalC := make(chan os.Signal)

	signal.Notify(SignalC, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
	go func() {
		for s := range SignalC {
			switch s {
			case syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
				close(exit)
				return
			}
		}
	}()

	<-exit
	socketio.Close(nil)
	os.Exit(0)
}

zishang520 avatar Jul 12 '24 10:07 zishang520