Messaging

Messaging includes enhancements to the beta feature, Platform Events, and a connector for Streaming API and Platform Events.
Available in: Performance, Unlimited, Enterprise, and Developer Editions

Deliver Customizable Events with Platform Events (Beta)

Platform events simplify the process of communicating changes and responding to them without writing complex logic. Publishers and subscribers communicate with each other through events. Multiple subscribers can listen to the same event and carry out different actions. This change applies to both Lightning Experience and Salesforce Classic. You can define platform events in Salesforce Classic only.
Note

Note

This release contains a beta version of Platform Events, which means it’s a high-quality feature with known limitations. For information on enabling this feature in your org, contact Salesforce. Platform Events isn’t generally available unless or until Salesforce announces its general availability in documentation or in press releases or public statements. We can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features. You can provide feedback and suggestions for Platform Events in the Success Community.

Use platform events to deliver secure and scalable custom notifications within Salesforce or from external sources. Define fields to customize your platform event. Your custom platform event determines the event data that the Force.com Platform can produce or consume.

By using platform events, publishers can send customized event data through Apex or an API. Subscribers can receive custom notifications from Salesforce or an external system and respond with actions using Apex or CometD clients. For example, a software system monitoring a printer can make an API call to publish an event when the ink is low. The custom printer event can contain custom fields for the printer model, serial number, and ink level. The event is processed in Salesforce by an Apex trigger that places an order for a new cartridge.

Define Your Platform Event

Define your platform event on the Platform Events page in the Salesforce user interface. From Setup, enter Platform Events in the Quick Find box, then select Platform Events.

Platform events are sObjects, similar to custom objects but with some limitations. Event notifications are instances of platform events. Unlike sObjects, you can’t update event notifications. You also can’t view them in the user interface. When you delete platform event definitions, they’re permanently removed.

Platform Events (Beta) Improvements in Spring ’17

Platform Events (Beta) was available last release. In this release, we made the following improvements and changes.

Enforcement of Event Field Attributes
New platform event records are validated to ensure that the attributes of their custom fields are enforced. Field attributes include the Required and Default attributes, the precision of number fields, and the maximum length of text fields.
Apex Testing Support
You can publish platform events in Apex tests and test their associated Apex triggers.
Trigger Number of Refires
A new class, EventBus.TriggerContext, provides information about the currently executing trigger. This information includes the number of times the trigger was refired with EventBus.RetryableException, enabling you to set a limit on the number of refires.
Packaging and Metadata API Support
You can add platform events and their associated Apex triggers to packages. Platform events are supported in Metadata API through the CustomObject type.
Limits
New limits are added for platform event publishing, event delivery to CometD clients, and event definitions.
Databinding.ObjectMapper No Longer Supported
The transform methods in the Databinding.ObjectMapper class have been deprecated and will be removed in a future release.

To learn more about platform events, see the Platform Events (Beta) Developer Guide.

Easily Subscribe to Streaming API in Java with EMP Connector

The Enterprise Messaging Platform (EMP) connector simplifies the process of subscribing to streaming events and platform events in Java through CometD. It also supports replaying past events with durable streaming. No need to write complex logic to implement those functions yourself! When you use EMP connector, call the provided functions to log in and subscribe to a channel.

EMP connector is based on Java 8 and uses CometD version 3.0.9. The connector supports PushTopic and generic streaming events, as well as platform events. It also supports username and password authentication and OAuth bearer token authentication.

Example

This Java example logs in with the specified username and password credentials and performs a handshake with CometD. Next, it subscribes to a PushTopic called myTopic with the channel name /topic/myTopic. The replay ID is initially set to the REPLAY_FROM_EARLIEST constant, which means that the subscription returns the oldest saved event (up to 24 hours) in addition to new events. The EmpConnector class is the main class that exposes the functions for starting a connection and subscribing. Event notifications are written to the output through the Java lambda expression (event -> System.out.println(..)).

// Replay from the start of the event window.
// May be any valid replayFrom position in the event stream.
long replayFrom = EmpConnector.REPLAY_FROM_EARLIEST; 

// Get parameters from login
BayeuxParameters params = login("<username>", "<password>");

// The event consumer.
// This lambda expression prints the received event to the output
// each time a notification is received.
Consumer<Map<String, Object>> consumer = event -> System.out.println(
        String.format("Received:\n%s", event));

// Instantiate the EMP connector
EmpConnector connector = new EmpConnector(params);

// Wait for handshake with Streaming API
connector.start().get(5, TimeUnit.SECONDS);

// Subscribe to a topic
// Block and wait for the subscription to succeed for 5 seconds
TopicSubscription subscription = connector.subscribe("/topic/myTopic", 
    replayFrom, consumer).get(5, TimeUnit.SECONDS);

// Here's our subscription
System.out.println(String.format("Subscribed: %s", subscription));

To use OAuth bearer token authentication instead, replace the BayeuxParameters value with the following. The URL corresponds to your Salesforce instance URL.

BayeuxParameters params = new BayeuxParameters() {

    @Override
    public String bearerToken() {
        return "<token>";
    }

    @Override
    public URL host() {
        try {
            return new URL("<URL>");
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException(String.format("Unable to create url: %s", argv[0]), e);
        }
    }
};

The following function calls end a subscription.

// Cancel a subscription
subscription.cancel();

// Stop the connector
connector.stop();

For a complete example, see the EMP-Connector GitHub repository.

And we saved the best for last: EMP Connector is an open-source project, which means that you can contribute to it with your own enhancements! Feel free to submit your contributions in the EMP-Connector GitHub repository.