1

I currently have a one-page Bottle project working through localhost:8080.

For the purposes of this question, assume that single page is naught but a basic short-polling chat, retrieving chatline objects from Python that contain only the sender's name and the body of the message.

Those chatline objects are stored in chat objects, with the project allowing multiple chats.

The chat and sender is determined by the URL. For example, if a chatline is sent from localhost:8080/chat/23/50, it is sent to chat 23 as sender 50, and localhost:8080/chat/23/* will display all chatlines of chat 23 in a basic overflow:auto div.

The current short-polling AJAX requests data from Python once every second. I want to make things more real-time and have decided to go with long-polling (although if you love HTML5 WebSockets, I wouldn't mind learning about them too).

My question is in two parts:

  1. How would I go about implementing a long-poll approach in such a chat system, preferably while still using Python's Bottle module?
  2. How would I then deliver the project through an actual server, accessible externally (i.e., not only from localhost)? Even making it available through LAN would be good.

I'm aware that long-polling can cause severe performance issues with servers such as Apache and would appreciate it if that fact could be factored into any answers; I'd like as scalable a solution as possible.

Any help is appreciated!

Djentleman
  • 1,137
  • 5
  • 13
  • 28

2 Answers2

2

I recently attended a presentation about a real-time client-server application that made great use of gevent on the Python/server side and socket.io on the client side. The speaker, Alexandre Bourget, released a gevent-socketio module ongithub, that can be used to make all the plumbing easier.

Everything worked with HTTP long polling only (but socket.io contains all the logic to switch to HTML5 WebSocket or Flash socket). Although the framework was Pyramid, I believe it should work with Bottle too!

Antoine Lassauzay
  • 1,557
  • 11
  • 12
  • Thanks! I'm currently looking into gevent-websocket and thinking more and more that websockets are the way to go, but I'll still be wanting to implement long-polling as a fallback if the client doesn't support websockets. Your links will help :) – Djentleman Oct 29 '12 at 23:20
  • Yes. And also, HTTP long polling seems easier to debug as you can see all the communication from client to server inside your usual HTTP debugger. Then you can switch to websocket in production :) With gevent-socketio, you can even manage your preferred method from the INI file of Pyramid ! – Antoine Lassauzay Oct 29 '12 at 23:27
  • Can you not see client->server communication within a debugger when using websocket? Shouldn't it just be a matter of console.log() on any messages being sent/received? Or does the continuous connection prevent that? – Djentleman Oct 29 '12 at 23:32
  • You can but you'll have to bring another tool or add your logs yes. Still a good asset in my opinion. For instance I use Charles proxy a lot or even Chrome dev tools that I can trigger when I am not on my dev env or without logging. – Antoine Lassauzay Oct 30 '12 at 00:26
1

I didn't try myself but I think you can use bottle together works with Tornado http://www.tornadoweb.org/ (see Tornado - mount Bottle app).

It is possible to make long-polling with Tornado. Look at the tornadio project https://github.com/mrjoes/tornadio.

You may also be interested in http://pypi.python.org/pypi/bottle-tornado-websocket. I never used this one but it looks like the thing you are looking for.

Tornado doc has a section about running in production : http://www.tornadoweb.org/documentation/overview.html#running-tornado-in-production

I hope it helps

Community
  • 1
  • 1
luc
  • 41,928
  • 25
  • 127
  • 172