How to implement SSE (Server Side Events) in SAP GW

Viewed 185

Intro

We got a requirement to create a web application that displays a table component, which should be refreshed after a specific DBTAB is modified (filtered to relevant keys only).

I made a research and concluded that in order to inform the client SSE (Server Side Event) would fit most.

Problem

I implemented a simple POC in ABAP. However, whenever the server is responding - a connection opens, the client receives the response (header & body), but then the connection immediately closes.

What I tried

  1. Request from our basis team to upgrade out GW to the latest (SAP S/4HANA 2020 with SAP_GWFND 755).

  2. Search Google and SAP KB for SSE, Server Side Event etc. Nothing found.

  3. Creation of an SICF node under default_host named SSE_POC.

  4. Definition of a single handler ZCL_CA_SSE_POC.

  5. implementation of interface IF_HTTP_EXTENSION in class ZCL_CA_SSE_POC.

  6. ABAP code:

    DATA: lo_http_server_net TYPE REF TO cl_http_server_net.
          lo_response        TYPE REF TO cl_http_response.
    lo_http_server_net ?= server. 
    lo_response        ?= lo_http_server_net->response.
    
    lo_response->set_header_field( name = 'Content-Type'
                                   value = 'text/event-stream' ).
    lo_response->set_header_field( name = 'Cache-Control'
                                   value = 'no-cache'          ). " just as a workaround 
    lo_response->set_header_field( name = 'Connection'
                                   value = 'keep-alive'        ). " just as a workaround
    
    lo_response->set_cdata( data = 'event: event\ndata:1\n\n' ).
    

Debugging

After calling my code, standard code (HTTP_DISPATCH_REQUEST:1726) reaches send_response method of server object. Inside it, it reaches:

   system-call plugin
       id 'HTTP'              value 'sendAndReturn'
       id 'name'              value conn_name
       id 'Stateful'          value stateful
       id 'LogonTime'         value m_authentication_time
       id 'LogonMethod'       value authentication_method
       id 'AuthorizationTime' value m_authorization_time
       id 'ExecutionTime'     value m_exe_time
       id 'EventHandlerExist' value l_eventhandler_exists
       id 'MessageOut'        value c_msg
       id 'RC'                value rc.

After running system-call - the client says an SSE connection opened and immediately closed.

Client code:

const evtSource     = new EventSource("http://<server>/sse_poc?sap-client=<mandt>");
evtSource.onmessage = ev => console.log(ev);
evtSource.onopen    = _ => console.log("Connection Opened");
evtSource.onerror   = ev => console.log(`error${ev.eventPhase === EventSource.CLOSED ? `: "Connection Closed"` : ''}`);

Chrome console:

Connection Opened
Error: "Connection Closed"
Connection Opened
Error: "Connection Closed"
... <every 3 seconds new pair>

Chrome network tab

Requests

Headers

EventStream

Postman

Body

Notes

Our system is on-premise.

0 Answers
Related