Skip to Content
Author's profile photo Lars Hvam

Custom MQTT client in ABAP

abapMQ

abapMQ is a custom open source implementation of MQTT 3.1.1, it currently implements all the packages defined in the protocol, and works over websockets and TCP., it works on 750sp01 and up.

It is now possible to connect to brokers on the internet and send or receive messages via the MQTT protocol:

There is still some work to be done, but the basics are in place, you can now connect your dishwasher(or other IoT devices) to your ABAP instance.

 

TCP Hacky Hack

ABAP supports 3 approaches for defining the TCP frame length,

A TCP frame can be of one of the following frame types, which has to be specified using the attribute frame_type in the frame structure (of type APC_TCP_FRAME).

  • Frame type is IF_APC_TCP_FRAME_TYPES=>CO_FRAME_TYPE_TERMINATOR: The TCP frame (message) is terminated by one or several bytes, e.g. line feed (LF) or carriage return line feed (CRLF). The terminator bytes have to be passed as hex code (00-FF) representation, e.g. for LF is this 0A or for CRLF is this 0D0A, to the terminatorattribute of the frame structure.
  • Frame type is IF_APC_TCP_FRAME_TYPES=>CO_FRAME_TYPE_FIXED_LENGTH: The TCP frame (message) has a constant length in bytes. The frame length in bytes has to be passed to the attribute fixed_length of the frame structure (as decimal number).
  • Frame type is IF_APC_TCP_FRAME_TYPES=>CO_FRAME_TYPE_LENGTH_FIELD: The frame length in bytes is part of the frame (message) itself and in a fixed position of the frame, i.e. a length field exists with a predefined offset and a predefined size. This type of frames (as an example RFC 1006) have the length field in position x of the frame, i.e. offset is x, and the length of the field length is of fixed length, i.e. 1,2,4 or 8 bytes. The length field specifies the whole/gross frame length. In this case the offset in bytes has to be passed to the attribute length_field_offset and the length of the length field in bytes has to be passed to the attribute length_field_length of the of frame structure.

However, in MQTT 3.1.1 the length of the package varies so none of the above can be used directly. To solve this I have set the frame length of all TCP packages to 1 byte, this is very evil, but it works, take care if you choose to use this in a productive environment. Websockets does not have this problem.

    DATA(ls_frame) = VALUE apc_tcp_frame(
      frame_type   = if_apc_tcp_frame_types=>co_frame_type_fixed_length
      fixed_length = 1 ).

    lo_tcp->mi_client = cl_apc_tcp_client_manager=>create(
      i_host          = iv_host
      i_port          = iv_port
      i_frame         = ls_frame
      i_event_handler = lo_tcp ).

 

Over’n’out

abapMQ is MIT licensed and can be found via http://abapmq.org, pull requests welcome :o)

 

Find more open source ABAP on http://dotabap.org

 

Assigned Tags

      6 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Rohit Gupta
      Rohit Gupta

      I have been looking for this

      Author's profile photo Lars Hvam
      Lars Hvam
      Blog Post Author

      Also see https://blogs.sap.com/2018/08/14/abap-at-sap-teched-2018/, looks like SAP also have some MQTT in store for TechEd

      And Jörg Müller has developed a message broker,  https://twitter.com/MDJoerg/status/1034570697752895488

      Author's profile photo Frank Radmacher
      Frank Radmacher

      That's right. SAP offers a native MQTT Client API as of ABAP Platform 1809. For further information, I recommend the Blog "MQTT client in ABAP Platform 1809". There you can also find a link to the official documentation and to a tutorial that teaches how to use the provided ABAP API.

      Btw, a recording of one of the TechEd lectures is also available.

      Best regards,
      Frank

      Author's profile photo Simone Milesi
      Simone Milesi

      Great job as usual Lars!

      It's really cool!

      Author's profile photo Lars Hvam
      Lars Hvam
      Blog Post Author

      thanks 🙂 also as usual, there is a lot missing, but this is a start

      Author's profile photo Jelena Perfiljeva
      Jelena Perfiljeva

      Thanks for sharing. For those who're also wondering what the heck is MQTT here is a summary (source):

      MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport.