Push/Subscribe Generic Events
Using Streaming Channel
This tutorial talks about how to push to and subscribe from the Salesforce streaming channel. Streaming channel is used to push the generic or custom message and subscribe those event/message for further processing
- A StreamingChannel that defines the channel, with a name that is case-sensitive
- One or more clients subscribed to the channel
Prerequisites:
- Installing Salesforce connector(by login into Anypoint exchange from studio you can install the Salesforce connector)
- Salesforce developer account
- Security token
- Consumer key and secret(If you are using the Salesforce connector to access an OAUTH API, you also need a consumer key and secret)
- Proper Salesforce Streaming API permissions enabled in your organization.
In last tutorial I have demonstrated how to create/publish channel in Salesforce. In this tutorial I am going to demonstrate how can we send the generic message to streaming channel and subscribe the same
Create project in anypoint studio and configure the listener to trigger the request. Here are the steps to Push and Subscribe Salesforce streaming channel
Step-1:
First will push the message to streaming channel using push generic event Salesforce operation
Steps to get the channel Id
Channel id was not visible on Streaming Channel Page, to add the streaming channel id create a new view
select the streaming channel id from available fields and move selected fields to display on main page and save the configuration
Now you can see Streaming Channel Id is visible on main page, you can use the streaming channel id and push the message
Drag and drop push generic event operation from mule palette
Configure the connection as basic authentication
Configure the channel id and create transform message to push to streaming channel
Dataweave Expression
%dw 2.0 output application/java --- [ { "payload": "Broadcast message data ", "userIds": [] } ]
Step 2:
There are two way to read the message from streaming channel one is subscribe channel and another option is replay channel, I will show both the methods to read the message from channel
Method-1
Subscribe the streaming channel using subscribe channel operation
Drag and drop subscribe channel operation from mule palette and provide the streaming channel name to subscribe from streaming channel.
Deploy the project and trigger the flow using postman/soapui
You can see below logs message has been published to streaming channel and received using subscribed channel for further processing
INFO 2020-05-12 14:40:02,216 [[MuleRuntime].io.06: [pusheventstochannel].pusheventstochannelFlow.BLOCKING @4637a2a4] [event: 599bb861-9430-11ea-91e5-040e3cd1eac7] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Message Sent
INFO 2020-05-12 14:40:14,915 [[MuleRuntime].cpuLight.04: [pusheventstochannel].pusheventstochannelFlow1.CPU_LITE @48ff6ec6] [event: 5b070830-9430-11ea-91e5-040e3cd1eac7] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Received Payload From Streaming Channel {data={payload=Broadcast message data , event={createdDate=2020-05-12T09:09:59.737Z, replayId=17}}, channel=/u/CustomChannel}
Method-2
Used the Replay Channel operation to read the message from streaming channel
Various Option given by replay channel
ALL: Subscriber receives all events, including past events that are within the 24-hour retention period and new events sent after the client subscribes
ONLY_NEW:
Subscriber receives new events that are broadcast after the client subscribes.
FROM_REPLAY_ID:
Subscriber receives all events after the specified event replayId.
Note: If you specify the ALL or ONLY_NEW the replay id values are ignored.
There is another option Resume from the Last Replay Id checkbox enables you to specify an automatic replay of stored events, based on the Replay ID of the last event processed by the connector.
Provide the channel name and Replay Option as ALL
Deploy the project, you can see below logs streaming channel read all the message published in last 24 hours
INFO 2020-05-12 15:22:49,748 [[MuleRuntime].cpuLight.06: [pusheventstochannel].pusheventstochannelFlow2.CPU_LITE @19d57167] [event: 57003e40-9436-11ea-b9ea-040e3cd1eac7] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Received Payload From Streming Channel :{data={payload=Broadcast message, event={createdDate=2020-05-12T09:01:54.756Z, replayId=9}}, channel=/u/CustomChannel}
INFO 2020-05-12 15:22:49,748 [[MuleRuntime].cpuLight.07: [pusheventstochannel].pusheventstochannelFlow2.CPU_LITE @19d57167] [event: 57019dd0-9436-11ea-b9ea-040e3cd1eac7] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Received Payload From Streming Channel :{data={payload=Broadcast message data , event={createdDate=2020-05-12T09:05:39.255Z, replayId=13}}, channel=/u/CustomChannel}
INFO 2020-05-12 15:22:49,748 [[MuleRuntime].cpuLight.03: [pusheventstochannel].pusheventstochannelFlow2.CPU_LITE @19d57167] [event: 56fe1b60-9436-11ea-b9ea-040e3cd1eac7] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Received Payload From Streming Channel :{data={payload=Broadcast message!!!!, event={createdDate=2020-05-12T08:56:00.378Z, replayId=3}}, channel=/u/CustomChannel}
INFO 2020-05-12 15:22:49,748 [[MuleRuntime].cpuLight.04: [pusheventstochannel].pusheventstochannelFlow2.CPU_LITE @19d57167] [event: 5700da80-9436-11ea-b9ea-040e3cd1eac7] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Received Payload From Streming Channel :{data={payload=Broadcast message data , event={createdDate=2020-05-12T09:04:18.238Z, replayId=11}}, channel=/u/CustomChannel}
INFO 2020-05-12 15:22:49,748 [[MuleRuntime].cpuLight.05: [pusheventstochannel].pusheventstochannelFlow2.CPU_LITE @19d57167] [event: 56ff7af0-9436-11ea-b9ea-040e3cd1eac7] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Received Payload From Streming Channel :{data={payload=Broadcast message, event={createdDate=2020-05-12T09:01:35.709Z, replayId=7}}, channel=/u/CustomChannel}
INFO 2020-05-12 15:22:49,748 [[MuleRuntime].cpuLight.01: [pusheventstochannel].pusheventstochannelFlow2.CPU_LITE @19d57167] [event: 56f0ade0-9436-11ea-b9ea-040e3cd1eac7] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Received Payload From Streming Channel :{data={payload=Broadcast message!!!!, event={createdDate=2020-05-12T08:53:47.803Z, replayId=1}}, channel=/u/CustomChannel}
INFO 2020-05-12 15:22:49,749 [[MuleRuntime].cpuLight.16: [pusheventstochannel].pusheventstochannelFlow2.CPU_LITE @19d57167] [event: 56feb7a0-9436-11ea-b9ea-040e3cd1eac7] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Received Payload From Streming Channel :{data={payload=Broadcast message, event={createdDate=2020-05-12T08:59:37.849Z, replayId=5}}, channel=/u/CustomChannel}
INFO 2020-05-12 15:22:49,749 [[MuleRuntime].cpuLight.08: [pusheventstochannel].pusheventstochannelFlow2.CPU_LITE @19d57167] [event: 57026120-9436-11ea-b9ea-040e3cd1eac7] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Received Payload From Streming Channel :{data={payload=Broadcast message data , event={createdDate=2020-05-12T09:07:21.680Z, replayId=15}}, channel=/u/CustomChannel}
INFO 2020-05-12 15:22:49,761 [[MuleRuntime].cpuLight.01: [pusheventstochannel].pusheventstochannelFlow2.CPU_LITE @19d57167] [event: 57037290-9436-11ea-b9ea-040e3cd1eac7] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Received Payload From Streming Channel :{data={payload=Broadcast message data , event={createdDate=2020-05-12T09:09:59.737Z, replayId=17}}, channel=/u/CustomChannel}
Sample application: pusheventstochannel sample application