antoniodipinto / ikisocket

🧬 WebSocket wrapper with event management for Fiber https://github.com/gofiber/fiber. Based on Fiber WebSocket and inspired by Socket.io
MIT License
123 stars 21 forks source link

BUG: kws.Conn.Headers("user-id") or kws.Params("user-id") not working #37

Closed talksik closed 10 months ago

talksik commented 10 months ago

Describe the bug We get empty string for headers and also params for the user id:

    app.Get("/ws", ikisocket.New(func(kws *ikisocket.Websocket) {

        // Retrieve the user id from endpoint
        userId := kws.Params("user-id")
        logrus.Infof("user id: %s", userId)

        // Add the connection to the list of the connected clients
        // The UUID is generated randomly and is the key that allow
        // ikisocket to manage Emit/EmitTo/Broadcast
        clients[userId] = kws.UUID

        // Every websocket connection has an optional session key => value storage
        kws.SetAttribute("user_id", userId)

        //Broadcast to all the connected users the newcomer
        kws.Broadcast([]byte(fmt.Sprintf("New user connected: %s and UUID: %s", userId, kws.UUID)), true)
        //Write welcome message
        kws.Emit([]byte(fmt.Sprintf("Hello user: %s with UUID: %s", userId, kws.UUID)))
    }))

Expected behavior Should properly grab the user-id from params and headers.

Screenshots image

antoniodipinto commented 10 months ago

Hi @talksik From Fiber documentation, when you use .Params("name"), you're referring to a route parameter not to a GET Query Parameter

Here you can find an example https://github.com/antoniodipinto/ikisocket/blob/master/examples/chat_room_example/main.go#L129

As you can see the userId := kws.Params("id") takes the data from the endpoint app.Get("/ws/:id")

Please refer to this example if you want to pass data as parameter https://github.com/antoniodipinto/ikisocket/blob/master/examples/chat_room_example/main.go

For the kws.Conn.Header not grabbing data, please remember that This will allow direct access to the following FastHTTP functions that is a layer deeper than Fiber. If you want to pass data from header, you should pass it from a .Use Middleware using Locals, in your case something like:

// Setup the middleware to retrieve the data sent in first GET request
    app.Use(func(c *fiber.Ctx) error {
        // IsWebSocketUpgrade returns true if the client
        // requested upgrade to the WebSocket protocol.
        if websocket.IsWebSocketUpgrade(c) {
            c.Locals("allowed", true)
                        // grab the user id from the header
                        userId :=  c.GetRespHeader("user-id")
                        // pass it to the websocket via Locals
                        c.Locals("user_id", userId)
            return c.Next()
        }
        return fiber.ErrUpgradeRequired
    })

And then grab it from locals

    app.Get("/ws", ikisocket.New(func(kws *ikisocket.Websocket) {

        // Retrieve the user id from endpoint
        userId := kws.Locals("user_id") // <====== HERE
        logrus.Infof("user id: %s", userId)

        // Add the connection to the list of the connected clients
        // The UUID is generated randomly and is the key that allow
        // ikisocket to manage Emit/EmitTo/Broadcast
        clients[userId] = kws.UUID

        // Every websocket connection has an optional session key => value storage
        kws.SetAttribute("user_id", userId)

        //Broadcast to all the connected users the newcomer
        kws.Broadcast([]byte(fmt.Sprintf("New user connected: %s and UUID: %s", userId, kws.UUID)), true)
        //Write welcome message
        kws.Emit([]byte(fmt.Sprintf("Hello user: %s with UUID: %s", userId, kws.UUID)))
    }))
talksik commented 10 months ago

@antoniodipinto thank you for the detailed response!! This is perfect! I will try this out :)