aerogo / aero

:bullettrain_side: High-performance web server for Go (2016). New alpha (2024) with even better performance is currently in development at https://git.akyoto.dev/go/web
MIT License
572 stars 33 forks source link

Question about EventStream #12

Closed iambudi closed 5 years ago

iambudi commented 5 years ago

Is there any best practice in aero to handle SSE for specific client only?

akyoto commented 5 years ago

1.)

You could, for example, store the user ID in your session when a user logs in.

ctx.Session().Set("userID", 123)

On the event handler, restore the user ID from the session cookie:

ctx.Session().Get("userID")

I usually write a wrapper function around this and call it GetUser(ctx).

2.)

Another method would be to create your own Context type...

type MyContext struct {
    aero.Context
    UserID string
}

and then pass your modified context to the next(ctx) call in the middleware. That way, all contexts will include a UserID field after casting the type.

I prefer the first method because it avoids an allocation, but the second method is also perfectly fine.

akyoto commented 5 years ago

Please let me know if anything is unclear, otherwise I'll keep this as closed now.