Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
alespad
Contributor

In the Part 2 we built a Node express app ,the Node server is up and running on port 7000

Now let's go with the ABAP!

We can make an Http Post request to the path /publish running on the node server on port 7000.


First we need to create a new Http destination using transaction SM59


On tab "Technical Settings Server" you specify our node server host localhost ,port :7000  and path  /publish 

On tab "Logon & Security" I set the basic authentication , user: SAP password: password



Test the connection


The express app.get( '/publish') responses with an ok status (200)



The following ABAP code makes an Http Post request with a json request Body (i'm using a simple concatenate statement but as always I suggest to use abap json document class by  se38


DATA: http_dest TYPE rfcdest VALUE 'NODE_PUBLISH'.
DATA: ld_client TYPE REF TO if_http_client.
DATA: ld_request TYPE REF TO if_http_request.
DATA: json TYPE STRING,
       http_status TYPE SY-SUBRC.

START-OF-SELECTION.

*Create the Http Client
   cl_http_client=>create_by_destination( EXPORTING
   destination = http_dest IMPORTING client = ld_client ).

*Prepare the Request
   ld_request = ld_client->request.

   CALL METHOD ld_client->request->set_method(
     if_http_request=>co_request_method_post ). "POST

   CALL METHOD LD_CLIENT->REQUEST->IF_HTTP_ENTITY~SET_HEADER_FIELD
     EXPORTING
       NAME  = 'Content-Type'
       VALUE = 'application/json'.


   json = '{"hello":"world"}'.

   CALL METHOD LD_CLIENT->REQUEST->IF_HTTP_ENTITY~SET_CDATA
     EXPORTING
       DATA = json.

   CALL METHOD LD_CLIENT->SEND
     EXCEPTIONS
       HTTP_COMMUNICATION_FAILURE = 1
       HTTP_INVALID_STATE         = 2
       HTTP_PROCESSING_FAILED     = 3
       HTTP_INVALID_TIMEOUT       = 4
       others                     = 5.

   if sy-subrc = 0.

     CALL METHOD ld_client->receive
       EXCEPTIONS
         http_communication_failure = 1
         http_invalid_state         = 2
         http_processing_failed     = 3
         OTHERS                     = 4.

     if sy-subrc = 0.

       ld_client->response->get_status( IMPORTING code = http_status ).

       if http_status = 200.
         WRITE: / 'MESSAGE SENT TO NODE'.
       else.
         WRITE : / 'HTTP STATUS KO' , http_status.
       endif.
     ENDIF.
   ENDIF.

   ld_client->close( ).



Executing the report, the message is sent correctly to node but there are no clients listening for it!

Now we can executing the simple client / javascript code  , opening 2 different Browsers (Firefox and Chrome) because we want to verify that our message will be triggered to all the connected clients

<script src="http://localhost:1337/socket.io/socket.io.js"></script>

<script>

var server = io.connect('http://localhost:1337');

server.on('SAP_Event', function (data) {

    console.log(JSON.stringify(data));

    alert('Hello ' + data.hello);

});

</script>


The node server is running...2 clients are connected...now we can executing the abap report again

(to get the complete code of Node server app check the previous blog)

Both the clients received the message triggered from ABAP and pushed by Node! The json message is received in realtime by the clients

PUSH THE COLOR!

A screencast to explain how it works is better then thousands words...in the final example i will trigger from abap a random name of color every 2 seconds, the color will be pushed to all the connected clients .Every client will use the received color to change the body background-color of the page!

The node server app doesn t change...because it simply emits the event with the json


Below the abap and the javascript..


ABAP

DATA: http_dest TYPE rfcdest VALUE 'NODE_PUBLISH'.
DATA: ld_client TYPE REF TO if_http_client.
DATA: ld_request TYPE REF TO if_http_request.
DATA: json TYPE STRING,
       http_status TYPE SY-SUBRC.

TYPES: begin of ty_color,
        color(10),
       end of ty_color.
DATA: v_color TYPE ty_color.
DATA: t_color TYPE TABLE OF ty_color,
       number_of_color type i.


START-OF-SELECTION.

   v_color-color = 'black'.
   append v_color to t_color.
   v_color-color = 'green'.
   append v_color to t_color.
   v_color-color = 'yellow'.
   append v_color to t_color.
   v_color-color = 'purple'.
   append v_color to t_color.
   v_color-color = 'blue'.
   append v_color to t_color.
   v_color-color = 'red'.
   append v_color to t_color.
   v_color-color = 'orange'.
   append v_color to t_color.
   v_color-color = 'brown'.
   append v_color to t_color.
   v_color-color = 'gray'.
   append v_color to t_color.


*Create the Http Client
   cl_http_client=>create_by_destination( EXPORTING
   destination = http_dest IMPORTING client = ld_client ).

*Prepare the Request
   ld_request = ld_client->request.

   CALL METHOD ld_client->request->set_method(
     if_http_request=>co_request_method_post ). "POST

   CALL METHOD LD_CLIENT->REQUEST->IF_HTTP_ENTITY~SET_HEADER_FIELD
     EXPORTING
       NAME  = 'Content-Type'
       VALUE = 'application/json'.


   DO 10 TIMES.

     WAIT UP TO 2 SECONDS.

     CALL FUNCTION 'QF05_RANDOM_INTEGER'
       EXPORTING
         RAN_INT_MAX = 9
         RAN_INT_MIN = 1
       IMPORTING
         RAN_INT     = number_of_color.

     READ TABLE t_color INTO v_color INDEX number_of_color.

     CONCATENATE '{"color":' '"' v_color '"}' INTO json.

     CALL METHOD LD_CLIENT->REQUEST->IF_HTTP_ENTITY~SET_CDATA
       EXPORTING
         DATA = json.

     CALL METHOD LD_CLIENT->SEND
       EXCEPTIONS
         HTTP_COMMUNICATION_FAILURE = 1
         HTTP_INVALID_STATE         = 2
         HTTP_PROCESSING_FAILED     = 3
         HTTP_INVALID_TIMEOUT       = 4
         others                     = 5.

     if sy-subrc = 0.

       CALL METHOD ld_client->receive
         EXCEPTIONS
           http_communication_failure = 1
           http_invalid_state         = 2
           http_processing_failed     = 3
           OTHERS                     = 4.

       if sy-subrc = 0.

         ld_client->response->get_status( IMPORTING code = http_status ).

         if http_status = 200.
           WRITE: / 'COLOR' , v_color-color ,  'SENT TO NODE'.
         else.
           WRITE : / 'HTTP STATUS KO' , http_status.
         endif.
       ENDIF.
     ENDIF.

   ENDDO.

   ld_client->close( ).


Client

<html>
<script src="http://localhost:1337/socket.io/socket.io.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script>
var server = io.connect('http://localhost:1337');
server.on('SAP_Event', function (data) {
    console.log(JSON.stringify(data));
    $('html').css('background-color',data.color);
});
</script>
</html>

and this is the screencast!


1 Comment