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()
}