Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Our architecture is HTTP servers (custom written) whereby custom clients send a HTTP request for some information and information is returned just as HTTP works. Now we need a special custom 'extension' which is a request which is a subscription for receiving asynchronous 'events' on a resource.

For example, the client sends an http request subscribing for events on some entity. As the 'entity' generates events they are passed to the http server and the http server must then lookup subscriptions for that entity and send the event message to all subscribed clients. Hope that makes sense.

So my questions are:

  • Has this been done before / or is there a standard I should be looking at?
  • If no standard, any suggestions on how to implement?
  • How does a http server send an unsolicited 'message' to a client?
share|improve this question
2  
So implementing the Observer pattern en.wikipedia.org/wiki/Observer_pattern? –  StuperUser Nov 18 '11 at 16:18
1  
Have you looked at SignalR or pubnub ? –  Esben Skov Pedersen Jan 24 at 14:32
    
Websockets allow to send unsolicited messages, but require to keep a connection open. –  Philipp Jan 25 at 9:59

5 Answers 5

up vote 0 down vote accepted

There is a draft for such a standard in HTML5, called "Server Sent Events"

Read all about it here: http://dev.w3.org/html5/eventsource/

share|improve this answer
    
This only applies to HTML5, though. OP specifically said that his server architecture is custom-built, so I doubt it serves fully HTML5-compliant websites. If that were the case, then OP would probably be better off just putting the notifications on the server-side and presenting them as a webpage element, like the SE inbox. –  Supuhstar Jan 24 at 0:48
2  
I don't see the relation of HTML5 the file format and HTTP the network protocol, these are two completely separate things. SSE is not HTML at all, its a different textual stream format. –  Evgeny Jan 24 at 14:43

You can't (that I know of) open an unsolicited CONNECTION (as in socket connection) to an HTTP client. You can, however, send unsolicited MESSAGES across a channel the client has left open. This concept is called Comet.

This pattern works nicely, and it should be able to provide the kind of pub-sub updates you are talking about.

share|improve this answer

It's really not what HTTP, as a connectionless protocol, was designed for so you may want to consider using something else for this type of communication, for example the XMPP protocol, or an enterprise messaging solution such as RabbitMQ.

share|improve this answer

HTTP protocol is an application layer protocol, and it sounds like what you want isn't really HTTP at all.

Just open a simple TCP/IP connection socket and have the client on a seperate thread listen for messages from the server. There are also plenty of frameworks as others are pointing out that will handle this complexity for you.

share|improve this answer

Here's how I would implement it:

  1. Client opens a TCP/IP connection with Server and (assuming that succeeds) sends an HTTP packet to Server that says Client would like to be notified when Entity has an update.
  2. Server adds Client to a list of observers, noting Client's IP address.
  3. Server waits for notifications from Entity.
  4. Upon receiving an update from Entity, Server establishes a TCP/IP connection with each observer, iteratively or otherwise, including Client.
  5. If connection with Client is successful, Server sends an HTTP packet with a notification that Entity has updated. Return to step 3.
    1. If connection with Client is unsuccessful, Client is put in a priority queue to receive that notification at a later time.
    2. When that predetermined time comes, Server again attempts step 4. If the connection is again unsuccessful, Client's priority is reduced. Server may choose to drop Client from the observer list if this priority drops too low.

Hope that helps!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.