As you know, nowadays most of the popular applications we are using are based on the real time notification as a main service. Real time update is the demand of the hour and every organization is moving in this direction to be ahead in the competition.
Real Time Notifications provide the end users and stake holders the latest up-to-date information about application data and state, without having the need to use the refresh button to get the updated data from the backend system.
In this article, we would learn how to create our first Real Time Client Server Communication using ABAP Push Channel (APC) and ABAP Messaging Channel (AMC). We would show you all the required components of this architecture and you can implement a working example using the step by step process.
The basic architecture is shown in this schema above:
Follow the below steps to implement a real time interaction scenario:
1. Create ABAP Push Channel
i. Go to SE80 transaction and create ABAP Push Channel
ii. Give some name to your ABAP Push Channel “ZTEST_APC” :
iii. Click on Generate Class and Service button as mentioned below:
Once the generation is successfully done, a new class ZCL_APC_WSP_EXT_ZTEST_APC will be created.
iv. You need to re-define the ON_START method and ON_MESSAGE method of the generated class to activate it. Hit the Re-define icon. The code snippet for ON_START is provided below.
The class should be Active now.
2. Create AMC (ABAP Messaging Channel)
Go to SE80, Create -> Connectivty -> ABAP Messaging Channel
Add the following information:
• Message Type Id (Example: /dunning_level_change)
• Activity Scope: System
Then add the following entry to list of Authorized Programs:
1. Authorized Program: ZCL_APC_WSP_EXT_ZTEST_APC (Class was created in the previous step)
Prog. Type: CLAS
Activity: Receive via APC WebSocket
2. Authorized Program: ZCREATE_USER (Must be created as a report, we’ll explain with further details in the next steps)
Prog. Type: REPS
Activity: Send
3. Implement the IF_APC_WSP_EXTENSION~ON_START Method in ZCL_APC_WSP_EXT_ZTEST_APC.
We have already informed you to re-define the ON_START method. Now simply add the following code:
method IF_APC_WSP_EXTENSION~ON_START.
DATA: lo_binding TYPE REF TO if_apc_ws_binding_manager.
DATA: lx_error TYPE REF TO cx_apc_error.
DATA: lv_message TYPE string.
* bind the APC WebSocket connection to an AMC channel
TRY.
lo_binding = i_context->get_binding_manager( ).
lo_binding->bind_amc_message_consumer(
i_application_id = ‘ZTEST_AMC’
i_channel_id = ‘/dunning_level_change’ ).
CATCH cx_apc_error INTO lx_error.
lv_message = lx_error->get_text( ).
ENDTRY.
endmethod.
In the above code snippet:
I_APPLICATION_ID is the name of the messaging channel we’ve already created.
I_CHANNEL_ID is the channel identifier
4. Create the report program: ZCREATE_USER
ZCREATE_USER : is an ABAP Report used for adding users to the ZUSER table. Once the addition is successfully done, we invoke the APC to notify the dunning level change.
REPORT ZCREATE_USER.
tables zuser.
data wa_user type zuser.
DATA:
lo_producer TYPE REF TO if_amc_message_producer_text,
lv_message TYPE string.
lv_message = ‘Dunning level has been changed. Reload the page to view the updated dunning levels’.
SELECTION-SCREEN begin of block block1 with frame title text-001.
PARAMETERS: p_user type string.
PARAMETERS: p_first type string.
PARAMETERS: p_last type string.
SELECTION-SCREEN end of block block1.
START-OF-SELECTION.
wa_user-FIRSTNAME = p_first.
wa_user-USERNAME = p_user.
wa_user-LASTNAME = p_last.
INSERT zuser from wa_user.
- Code to invoke the APC to notify the dunning level change
TRY.
lo_producer ?= cl_amc_channel_manager=>create_message_producer(
i_application_id = ‘ZTEST_AMC’
i_channel_id = ‘/dunning_level_change’ ).
lo_producer->send( i_message = p_first ).
CATCH cx_amc_error INTO DATA(lx_amc_error).
RETURN.
ENDTRY.
ZUSER is a simple table where we have our data.
- CREATE_MESSAGE_PRODUCER will be triggered once a new entry is added to the table with two parameters i_application_id, i_channel_id.
- SEND method will send the customizing message to the frontend part.
We created a simple SAPUI5 application with worklist with list of users stored in the ZUSER database table:
In the onInit method in the worklist controller we add the initDunningRunFeed method to open the connection using the WebSocket between SAPUI5 Application and APC which will raise the IF_APC_WSP_EXTENSION~ON_START method (which is already implemented):
initDunningRunFeed: function() {
var that = this ;
varhostLocation = window.location,
socket, socketHostURI, webSocketURI;
if (hostLocation.protocol === “https:”) {
socketHostURI = “wss:”;
} else {
socketHostURI = “ws:”;
}
socketHostURI += “//” + hostLocation.host;
webSocketURI = socketHostURI + ‘/sap/bc/apc/sap/ztest_apc’ ;
// varoSocket = this.oSocket = new sap.ui.core.ws.SapPcpWebSocket(webSocketURI,
// sap.ui.core.ws.SapPcpWebSocket.SUPPORTED_PROTOCOLS.v10);
socket = new WebSocket(webSocketURI);
socket.onopen = function() {};
socket.onmessage = function(dunningRunFeed) {
if (dunningRunFeed.data !== undefined) {
jQuery.sap.require(“sap.m.MessageBox”);
sap.m.InstanceManager.closeAllDialogs();
that.getOwnerComponent().getModel().refresh();
sap.m.MessageBox.show(
‘You ve added a new user : ‘+ dunningRunFeed.data,
sap.m.MessageBox.Icon.INFORMATION,
“APC Notification”, [sap.m.MessageBox.Action.OK]
);
}
};
socket.onclose = function() {};
},
If a new entry is added to the ZUSER data base table, the socket.onmessage callback will be triggered. Inside this callback we perform a new refresh to the model to make the list updated.
We hope, you know how to create a simple OData Service and redefine the method USERSET_GET_ENTITYSET.
<code>method USERSET_GET_ENTITYSET.
data ls_user type zuser.
data lt_user type TABLE OF zuser.
select * from ZUSER into CORRESPONDING FIELDS OF TABLE LT_USER.
MOVE-CORRESPONDING LT_USER TO ET_ENTITYSET.
endmethod.</code>