Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🤔 Use Context in websocket Conn? #205

Closed
hyingreborn opened this issue Mar 5, 2020 · 10 comments
Closed

🤔 Use Context in websocket Conn? #205

hyingreborn opened this issue Mar 5, 2020 · 10 comments
Assignees

Comments

@hyingreborn
Copy link

hyingreborn commented Mar 5, 2020

Question description

Code snippet (optional)
main.go

package main

import "github.com/gofiber/fiber"

func main() {
  app := fiber.New()
  // JWTAuth is custom middleware, will use c.Local("claims") to store token claims in context
  app.Use(JWTAuth())
  app.Websocket("/ws", WSHandler)
}

wsHandler

func WSHandler(c *fiber.Conn) {
	for {
		mt, msg, err := c.ReadMessage()
		if err != nil {
			log.Println("read:", err)
			break
		}
		log.Printf("recv: %s", msg)
		// if this is a normal handler
		claims := c.Locals("claims").(*middleware.JwtClaims)
		msg = fmt.Sprintf("Hello! Your ID is: %s", claims.UserID)
		err = c.WriteMessage(mt, msg)
		if err != nil {
			log.Println("write:", err)
			break
		}
	}
}
@hyingreborn hyingreborn changed the title 🤔 how use c.Locals() in a websocket conn? 🤔 How use c.Locals() in a websocket conn? Mar 5, 2020
@Fenny
Copy link
Member

Fenny commented Mar 5, 2020

This is something that was on my mind when implementing websockets into fiber, we might move the Conn inside the Ctx (c.Conn) or pass an optional context handler in the callback (func(conn *Conn, ctx ...*Ctx). Ideas are welcome, I will do some testing this weekend. I will keep you updated 👍

@Fenny Fenny changed the title 🤔 How use c.Locals() in a websocket conn? 🤔 Use Context in websocket Conn? Mar 5, 2020
@hyingreborn
Copy link
Author

@Fenny Thanks a lot!

@Fenny
Copy link
Member

Fenny commented Mar 11, 2020

Locals will be added to the Conn struct in v1.8.3. I will close this issue upon release.

@coderbuzz
Copy link

Just landed here, so Fiber has websockets support already? What ws lib is used? Most performed Gobwas/ws?

@koddr
Copy link

koddr commented Mar 12, 2020

@coderbuzz hi!

Fiber use Fasthttp Websocket lib via middleware https:/gofiber/websocket

@coderbuzz
Copy link

@koddr hi,

Comes from NodeJS, in Node there is uWebSockets library which very perform.

http benchmark

WS

Sadly the author won't make the library available in Go. Based on benchmark above; Gobwas/ws seems most performed.

Curious any benchmark result for Fasthttp Websocket vs Gobwas/ws?

@Fenny Fenny mentioned this issue Mar 14, 2020
@Fenny
Copy link
Member

Fenny commented Mar 14, 2020

@coderbuzz, In my opinion uWebSockets is currently only useful for route levels. When we talk about proper concurrency inside the request handler, the benchmarks don't lie.

Lets for example calculate 10 MD5 hashes (which is super fast) to simulate some processing that might happen:

wrk -t1 -c400 -d10s http://127.0.0.1:8080/

uWebsocketjs

// Thread Stats   Avg      Stdev     Max   +/- Stdev
// Latency     9.36ms    3.30ms  48.12ms   67.27%
// Req/Sec    43.02k     2.47k   47.12k    74.00%
const crypto = require('crypto');
const uws = require('uWebSockets.js');

var app = uws.App()
app.get('/*', (res, req) => {
  for (let i = 0; i < 10; i++) {
    const h = crypto.createHash('md5').update('foobar').digest('hex');
    if (h !== '3858f62230ac3c915f300c664312c63f') {
      throw h;
    }
  }
  res.end('Hello World!');
})
app.listen(8080, () => {});

Fiber

// Thread Stats   Avg      Stdev     Max   +/- Stdev
// Latency     2.82ms  363.98us   6.02ms   84.98%
// Req/Sec   140.72k     5.03k  148.43k    75.00%
package main

import (
  "crypto/md5"
  "encoding/hex"
  "github.com/gofiber/fiber"
)

func main() {
  app := fiber.New()
  app.Get("/", func(c *fiber.Ctx) {
    for i := 0; i < 10; i++ {
      b := md5.Sum([]byte("foobar"))
      h := hex.EncodeToString(b[:])
      if h != "3858f62230ac3c915f300c664312c63f" {
        panic(h)
      }
    }
    c.SendString("Hello, World!")
  })
  app.Listen(8080)
}

@Fenny
Copy link
Member

Fenny commented Mar 14, 2020

@hyingreborn Locals are fixed in #224 using https:/gofiber/websocket

@kuchaguangjie
Copy link

So, what's the solution now ?

@kuchaguangjie
Copy link

Seems just use conn.Locals().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants