Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1021141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:16:00+00:00 2026-05-16T11:16:00+00:00

Note: I’m more interested in understanding general Go concepts/patterns, rather than solving this contrived

  • 0

Note: I’m more interested in understanding general Go concepts/patterns, rather than solving this contrived example.

The Go (golang) WebSocket package provides a trivial echo server example, which condenses down to something like this:

func EchoServer(ws *websocket.Conn) { io.Copy(ws, ws); }

func main() {
 http.Handle("/echo", websocket.Handler(EchoServer));
 http.ListenAndServe(":12345", nil);
}

The server handles simultaneous connections, and I’m trying to upgrade it to a basic chat server by echoing the input to all connected clients.

How would I go about providing the EchoServer handler access to each of the open connections?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:16:01+00:00Added an answer on May 16, 2026 at 11:16 am

    A quick little almost-functional example to give you an idea

    var c = make(chan *websocket.Conn, 5) //5 is an arbitrary buffer size
    var c2 = make(chan []byte, 5)
    func EchoServer(ws *websocket.Conn) {
        buff := make([]byte, 256)
        c <- ws
        for size, e := ws.Read(buff); e == nil; size, e = ws.Read(buff) {
            c2 <- buff[0:size]
        }
        ws.Close()
    }
    func main() {
        go func() {
            var somekindofstorage
            for {
                select {
                case newC := <-c:
                    somekindofstorage.Add(newC)
                case msg := <-c2:
                    for _, v := range somekindofstorage {
                        if _, e := v.Write(msg); e != nil { //assuming the client disconnected on write errors
                            somekindofstorage.Remove(v)
                        }
                    }
                }
            }
        }()
        http.Handle("/echo", websocket.Handler(EchoServer));
        http.ListenAndServe(":12345", nil);
    }
    

    This starts a goroutine that listens on two channels, one for new connections to add and one for messages to send to all active connection. somekindofstorage could be a map or a vector.

    Edit:

    Alternatively, you could just store all connections in a global map and write to each from EchoServer. But maps aren’t designed to be accessed concurrently.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: since I asked this question, .ix still exists but more or less has
Note, this is not a duplicate of .prop() vs .attr() ; that question refers
(Note: This is not a question about what is the best way with code
Note: Not sure if this is the right stack, please tell if I should
NOTE: This is a followup to my question here. I have a program that
NOTE: This is an old question and the answers here no longer works (since
Note: I understand this is not strictly about development, but in the earlier threads
Note: I've read this and its not quite what I'm looking for: I have
NOTE Due to the successful outcome of this thread, the documentation at the provided
Note the tag: VBA , not VB6, not VB.NET. This is specific to VBA

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.