Push technology is a model of Internet-based communication in which information transfer is initiated from a server to the client. Also called the publish/subscribe model, this type of communication is the opposite of pull technology in which a request for information is made from a client to the server. The information that’s sent by the server is typically specified in advance. When using Streaming API, you specify the information the client receives by creating a PushTopic. The client then subscribes to the PushTopic channel to be notified of events that match the PushTopic criteria.
In push technology, the server pushes out information to the client after the client has subscribed to a channel of information. In order for the client to receive the information, the client must maintain a connection to the server. Streaming API uses the Bayeux protocol and CometD, so the client to server connection is maintained through long polling.
The Bayeux protocol and CometD both use long polling.
- Bayeux is a protocol for transporting asynchronous messages, primarily over HTTP.
- CometD is a scalable HTTP-based event routing bus that uses an AJAX push technology pattern known as Comet. It implements the Bayeux protocol.
There is nice jQuery plugin available on web which implements Bayeux protocol and CometD.
Common Terms used in Streaming API:
- Event : Either the creation of a record or the update of a record. Each event may trigger a notification.
- Notification : A message in response to an event. The notification is sent to a channel to which one or more clients are subscribed.
- PushTopic : A record that you create. The essential element of a PushTopic is the SOQL query. The PushTopic defines a Streaming API channel.
Example used in this article :
I want notifications about all opportunity whose status is won.
First step to implement any streaming API is to create the Push Topic, which is going to subscribed by all the clients.
To create PushTopic, we will need the developer console of the Salesforce and assume that the name of the topic is “WonOpportunity”.
PushTopic pushTopic = new PushTopic(); pushTopic.ApiVersion = 23.0; pushTopic.Name = 'WonOpportunity'; pushTopic.Description = 'Notify if the Opportunity won'; pushTopic.NotifyForOperations = 'All'; pushTopic.NotifyForFields = 'Referenced'; pushtopic.Query = 'Select o.OwnerId, o.Name, o.IsWon, o.Id, o.Amount From Opportunity o WHERE o.IsWon = true'; insert pushTopic; System.debug('Created new PushTopic: '+ pushTopic.Id);