Websockets Implementation

I am able to send real time messages to everyone in a chat room by creating a web socket handler and then adding clients to a hashmap such as ActiveClients[client] and then using this code which messages everyone in the ActiveClients:

for cs, _ := range ActiveClients {
            if err = websocket.Message.Send(cs, reply); err != nil {

            // we could not send the message to a peer
            log.Println("Could not send message to ", c.clientIP, err.Error())

I am using JavaScript sockets to connect on the front end and it sends JSON data to the back end through a web socket handler. I am using the package “golang.org/x/net/websocket” for the back end.

Is it better to use the same socket connection for everything(think chatroom, game room, private messaging, etc all sharing the same socket handler). Or should I make multiple web socket handlers to handle each back end logic?

I have different web pages connecting to the same wss://mywebsite.com:port socket connection, one web page handles chat, one handles real time game and private chat. I have one web socket handler in the back end which unmarshal the JSON and then it uses switch statements to identify if the JSON is a public chat message, if its a game move, or if its a private message etc. Is this an efficient implementation of web sockets?

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.