Pushlet-open-source comet framework
Pushlet is an open-source comet framework. It has a lot to learn from in terms of design and has great reference value for developing lightweight comet applications.
Observer Model
Pushlet uses the observer model: the client sends a request and subscribes to events of interest. The server assigns a session ID for each client as a tag, the event SOURCE sends new events to the subscriber's event queue in multicast mode.
Client JavaScript Library
Pushlet provides Ajax-based JavaScript library files for "server push" in long polling mode, and IFRAME-based JavaScript library files for "server push" in stream mode ".
The javascript library has done a lot of encapsulation work:
- Define the communication status of the client:
STATE_ERROR
,STATE_ABORT
,STATE_NULL
,STATE_READY
,STATE_JOINED
,STATE_LISTENING
;
- Save the session ID allocated by the server. The session ID is attached to each request after the connection is established to indicate the identity;
- Provided
join()
,leave()
,subscribe()
,unsubsribe()
,listen()
And so on
APIS for page calls;
- Provides JavaScript function interfaces for response processing.
onData()
,onEvent()
...
Web pages can easily use the APIS encapsulated by these two JavaScript library files to communicate with the server.
Format of client-server communication information
Pushlet defines a set of information formats for communication between customers and servers, in XML format. Defines the types of requests sent by the client:join
,leave
,subscribe
,unsubscribe
,listen
,refresh
And RESPONSE event type:data
,join_ack
,listen_ack
,refresh
,heartbeat
,error
,abort
,subscribe_ack
,unsubscribe_ack
.
Server-side event queue management
Pushlet is implemented using Java Servlet on the server side. Its data structure design framework can still be applied to backend clients written in PHP and C.
Pushlet allows the client to select the stream, pull (long polling), and polling modes. The server performs different processing when reading the event queue (fetchevents) according to the method selected by the customer. In "polling" ModefetchEvents()
Will return immediately ." Stream "and" pull "modes use the blocking mode to read events. If it times out, it will send a" Heartbeat "event to the client, if it is in" pull "mode, the "Heartbeat" and "refresh" events are sent to the client together, notifying the client to send a new request and establish a connection.
Session management between customer servers
The server sends messages on the client.join
When a request is sent, a session ID is assigned to the client and sent to the client. Then, the client sends a session IDsubscribe
Andlisten
Request. The server maintains a subscribed topic set and event queue for each session.
The event source on the server sends the newly generated events to the event queue of each session (that is, the subscriber) in multicast mode.
Official Website: http://www.pushlets.com/