Search code examples
pythonwebsockettornado

Dealing with HTTP and websocket connections on the backend


I am working on a game I want to support over iOS/Android/Browser and thinking Websockets is what I want to use for the communication. I use python and so found that I should be using Tornado.

I am trying to understand websockets a little better and their integration in browsers.

  1. Will the messages over the websocket connection also contain the HTTP cookies for the connection? If not can I send it?
  2. How is the HTTP connection for the web page linked to the websocket connection? I mean how will I know they are coming from the same webapp on the server side?
  3. The Tornado wiki page says in the performance section that Tornado can be set up with nginx as the front end. How does that work? I thought Tornado and nginx have to be running on separate machines since both listen on port 80 and also because nginx does not understand WS protocol. What am I missing?

Also it will be great if someone can point me to any resources I can read up on about either Tornado or websocket that could help me.


Solution

    1. The websocket is setup by sending an ordinary http request to the server, this request will contain all the stored cookies for the domain. If you do a native implementation for e.g. Android you can use libraries like Autobahn|Android, the API allows you to set cookies for the websocket handshake.

    2. You can set a cookie when first loading the page to maintain a session identifier.

    3. In that scenario they would be running 4 Tornado instances (on different ports, but not port 80) and Nginx on port 80 as a load-balancer, spreading the incoming client requests to the Tornado instances, see running Tornado and Nginx on same server for a configuration example. Recent versions of Nginx does support websockets, see e.g nginx + python + websockets.