HTTP streaming comet client

(F) #1


I’m trying to implement a client for comet protocol.
In “streaming” mode the server returns all results on single http channel using chunked transfert.

Cache-Control: no-cache
Connection: keep-alive
Pragma: no-cache
Transfer-Encoding: chunked
Content-Type: application/json

With the following code, I can receive the first response from the commands that creates the server, but nothing else. The other following messages are lost.

	p := make([]byte, 50)
	rd := bufio.NewReader(body)
	for {
		l, err := rd.Read(p)
		if err != nil {
			log.Fatal("Read Error:", err)
		fmt.Println(l, string(p))

And after 1 minute the error message:

2019/09/24 17:37:13 Read Error:unexpected EOF
exit status 1

Looks like the channel is closed after the first response.
Any idea?

(Darius Mejeras) #2

I am not familiar with Comet, but it looks like you want to implement Server-Sent Events. Most modern browsers support SSE streams and there are libraries for it.

(F) #3

Thanks for the suggestion. Yes I believe the principle is similar.

I’ve tried to grab the reader code from, and the result is the same: “Read Error:unexpected EOF” Whatever reader or method I use, I received the same error when the 2nd message is sent. So I believe my issue is in lower level.

For reference, here is the doc about comet/bayeux protocol:

(F) #4

Unfortunately I’m getting nowhere:
I have simple loop to grab the messages

rsp, err := c.http.Do(req)
if err != nil {
	return err

defer rsp.Body.Close()
dec := json.NewDecoder(rsp.Body)

for {
	var messages []message
	err = dec.Decode(&messages)
	if err != nil {
		if err == io.EOF {
		return err

but after the first message I get EOF, probably because the first message is complete.


How can I reset the reader so it waits for another message, on the same connection (without new request)

(Matthias Mädel) #5


I used the following template and it is fully functional…

you must consider max event channel number is 6!


(F) #6

Thanks Matthias but I don’t have control over the server.
I’m trying to connect my client to it using the bayeux protocol.