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

193
Views
Adding information about the client on the initial Websocket handshake

I'm currently creating a distributed chat application. Everything works fine, meaning it's possible to send messages between the clients and the server and have it broadcasted appropiately.

However, at the moment only the actual message is sent to the server. I would like to add information about the user sending the message aswell.

I could add this information whenever I send a new message, but I would prefer if I could add this information during the initial handshake and then save this information on the backend.

I've thought about sending some information in the URL, but as I only instantiate the websocket once, this does not seem like the way to go. Similarly, I thought about adding the information as the body of the request, but I read that having a body on a GET request is usually not recommended.

So my question is, am I trying to do something that I should not be going for? Should I just send information about the client on each new message that is sent to the server?

Currently, my client looks like this:

const socket = new WebSocket("ws://localhost:8080/ws");

const connect = (cb) => {
    console.log("Attempting Connection...")

    socket.onopen = () => {
        console.log("Successfully Connected");
    }

    socket.onmessage = (msg) => {
        console.log(msg)
        cb(msg);
    }

    socket.onclose = (event) => {
        console.log("Socket Closed Connection: ", event);
    }

    socket.onerror = (error) => {
        console.log("Socket Error: ", error);
    }
};

const sendMsg = (msg) => {
    console.log("Sending msg: ", msg);
    socket.send(msg);
}

And the initial connection on the backend is handled by the following:

func serveWs(pool *websocket.Pool, w http.ResponseWriter, r *http.Request) {
    fmt.Println("WebSocket Endpoint Hit")

    conn, err := websocket.Upgrade(w, r)

    if err != nil {
        fmt.Fprintf(w, "%+V\n", err)
    }

    client := &websocket.Client{
        Name: "?????", // Obviously, I would like the actual name to be here.
        Conn: conn,
        Pool: pool,
    }

    pool.Register <- client

    client.Read()
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The normal thing to do here as selbie pointed out is to expect from the client a special first message through WebSocket. If that message is not recived or does not meet the requirements the server ends the WebSocket conn.

Using socket.onopen is very common for this task.

More on why you cant put headers: HTTP headers in Websockets client API

about 4 years ago · Juan Pablo Isaza Report
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!