Projects‎ > ‎

Push Server and Real-time App

Implement an HTTP push server using the "Pushbutton Web" concepts discussed in class.  

Then, write a simple web app that uses it.

Server Requirements

There are no specific requirements for your server, but here is a sample design:
  • Push data into a named queue, e.g. POST /queues/1
  • Poll data from a named queue, e.g. GET /queues/1?since=5
A push request adds a message to the corresponding queue.  A poll request pops off and returns a message.  If the queue is empty, the poll request should block in order to support long-polling.

In order to implement these named queues, you'll need to map names to a blocking queue data structure. In Go, this looks like:

var queues = make(map[string]chan []byte)

Then:

queues["id"] = make(chan []byte, 100)  // construct blocking queue for 100 messages
queues["id"] <- []byte("message body")  //push a message into the queue
response := <-queues["id"] // pop a message from the queue (block if empty)

In Python, you must use the blocking queue in the Queue module:

from Queue import Queue

queues = dict() // construct dictionary
queues["id"] = Queue(100) // construct blocking queue for 100 messages
queues["id"].put("message body")  //push a message into the queue
response = queues["id"].get(True) // pop a message from the queue (block if empty)

Notice that blocking request handlers make it necessary for your server to be multithreaded.  Otherwise, blocking in one request would prevent the server from responding to any other requests!  The whole server would block.  Python's SimpleHTTPServer is not multithreaded, so you must add this capability.  Luckily, this is easy with mix-ins:

class ThreadingHTTPServer (SimpleHTTPServer, ThreadingMixIn):
   pass

Now use ThreadingHTTPServer where ever you'd use SimpleHTTPServer.

Go's built-in HTTP server is already multithreaded, so you don't need to do anything special there.

Depending on your application, your queues might store raw data (e.g. []byte or string), objects (e.g. a Message struct), or arbitrary JSON (e.g. interface{}).

App Requirements

You can do whatever you want for the app.  Here are some ideas:
  • An instant messaging app.  To implement this, each named queue corresponds to a user's "inbox". One user sends a message to another user by pushing a messaging into his/her queue.  The message should contain information about who sent the message.
  • A group messaging app.  To implement this, each group has a unique ID.  Users can be "invited" to a group just by sharing the group ID with them.  All messages to the group are pushed into the same queue.  Each message should contain information about who sent the message.
  • A collaborative document or whiteboard app.  This is just like the group messaging app, except the messages encode changes to a document or lines on a whiteboard.
  • A real-time forum.  Users can create topics and post to topics.  New posts appear instantly.
  • A multiplayer or MMO game.

Comments