Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
Making a client side Websocket connection request to a Go HTTP endpoint that authenticates and upgrades request to WS

The title is basically the question I have. I was developing my Go server, and added the gorilla/ws library to the endpoint http://qwerty:1234/api/ws to handle connection upgrade requests.

This endpoint has a middleware before it that validates the Auth header's bearer token.

While developing I could customize the WS connection request in Postman to include the Auth header. And it successfully connected to the WS endpoint.

I'm new to WS. And I tried to develop the client side WS connection, lo, you can't add headers.

Is there a workaround to do what I am trying? Or is there a way to emulate what Postman does? Is there a Javascript WS library that I can use to do what I am trying?

Server code

// Router
r.Route("/api", func(r chi.Router) {

    r.Group(func(r chi.Router) {
        r.Use(utils.AuthMiddleware)

        r.Get("/contacts", controller.GetContacts)
        r.Get("/ws", websocket.Handler)
        r.Get("/ws/clients", websocket.TestHandler)

// Socket connection upgrade
func Handler(w http.ResponseWriter, r *http.Request) {
    userDetails := r.Context().Value("userDetails").(jwt.MapClaims) // Populated by middleware

    userId := int64(userDetails["UserId"].(float64))
    if _, userPresent := wsClientsById[userId]; userPresent {
        http.Error(w, "User already connected", http.StatusForbidden)
        return
    }

    conn, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
        http.Error(w, "Error establishing Websocket connection", http.StatusInternalServerError)
        fmt.Println(err)
        return
    }
    conn.SetReadLimit(4096)
    wsClientsById[userId] = conn
    wsClientsByConn[conn] = userId

    go wsConnHandler(conn)
}
about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!