google-app-enginegochannel-api

Channel api: persistent disconnections and connections


I am using Channel API, and after about 1 minute after channel's opening in terminal I see

INFO     2015-10-20 11:18:08,489 module.py:786] default: "POST /_ah/channel/disconnected/ HTTP/1.1" 200 2278
2015/10/20 11:18:10 handlerMain executed
INFO     2015-10-20 11:18:10,482 module.py:786] default: "POST /_ah/channel/connected/ HTTP/1.1" 200 2279
2015/10/20 11:18:13 handlerMain executed
INFO     2015-10-20 11:18:13,486 module.py:786] default: "POST /_ah/channel/disconnected/ HTTP/1.1" 200 2279
2015/10/20 11:18:14 handlerMain executed
INFO     2015-10-20 11:18:14,482 module.py:786] default: "POST /_ah/channel/connected/ HTTP/1.1" 200 2279

and so on..

my .go file

func init() {
    http.HandleFunc("/", handlerMain)
}
func handlerMain(w http.ResponseWriter, r *http.Request) {
            log.Println("handlerMain executed")

            c := appengine.NewContext(r)
            tok, err := channel.Create(c, "123")
            if err != nil {
                    panic(err)
            }   
            templ := template.Must(template.ParseFiles("./templates/posts.html"))
            err = templ.Execute(w, map[string]string{
                    "token": tok,
            })  
            if err != nil {
                    panic(err)
            }   
 }

javascript from my .html file

  <script>
    channel = new goog.appengine.Channel('{{.token}}');
    socket = channel.open();
    socket.onopen = onOpened;
    socket.onmessage = onMessage;
    socket.onerror = onError;
    socket.onclose = onClose;
  </script>

If it where just simple notifications, I could have handle it, but this disconnections reexecutes my handlerMain() function, and in this function I want to start goroutine which will send messages to client, in this case I will have multiple repetitions of messages in my html page.

Any thoughts? Is there alternative for Channel API? I know that websockets unfortunately doesn't work with GAE

UPDATE: In browser I see GET XMLHttpRequests are sent every second to the

http://localhost:8080/_ah/channel/dev?command=poll&channel=237c7242478266a2856d947decce4b55-channel-2105948409-1445426965-123&client=1

with header Connection : "keep alive";

When I switch browser to another tab, after a few seconds these requests are stopped(or very much slowed down), and I start recieve this connection/disconnection notifications. If i jump back to the page, requests are sent again every second, and no notifications.


Solution

  • Channels connection/disconnections appears only on my macbook, not on the PC. Looks like it have something to do with app-engine development server, here more details: https://groups.google.com/forum/#!topic/google-appengine-go/dLe2UvzUgdA