Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
Sign up to join this community
Anybody can ask a question
Anybody can answer
The best answers are voted up and rise to the top
I’m working on a project which focuses on simple HTTP-related actions (to get my feet wet really). I’m using the
net/http package, which I thought was very convenient for almost all requirements except that it seems a
http.Server gives instructions to
http.Client if and only if the client has established a connection.
I have learnt that
- I can use
x/net/websocketto solve this problem.
- I can choose to use some long polling technique(I guess).
- I have also considered spawning a routine by my client to connect to the server repeatedly to simulate the notifications, like every 200 milliseconds.
I have implemented most of the functions using
net/http, so I’m not sure if I should really move to
My question is: what is the common paradigm that Go engineers will use in this situation? Which is the most doable idea, or is there a better solution?
After some research, I have decided to stick to the
http package and use strategy 3. With that being said, I will post what I found, just in case anyone finds them helpful:
- The WebSocket is a VERY good choice for similar problems, when both the clients and servers need to write/read the connection. However, the
github.com/gorilla/websocketseems to be a better library compared to
x/net/websocket, and some helpful examples using gorilla can be found here.
- The long-polling is another commonly used technique (according to the blogs I studied). But this requires more attention for errors, like when the client crashed and the tcp connection is closed by one side. The server must have some functions for such scenarios to handle requests gracefully.
- Using a constantly called
http.Getis NOT a good choice! If a server using this technique has too many on-list clients, all the requests and connections from the clients may cause the server to crash, not mentioning some OS may limit the number of tcp connections a machine can hold. The reason I choose this idea is that I know I will never publish my server, so basically my server does not have to worry about performance. Obviously I will choose the easiest one.
Not the answer you’re looking for? Browse other questions tagged http go or ask your own question.