Search code examples
javaspringspring-bootwebsocketasyncapi

AsyncAPI Websockets Spring generated code and implementation


I want to generate code from AsyncAPI specification and try out how it works. My goal is to have only 1 socket open for each client and over that connection send/receive different types of messages. I couldnt find any example that would show me how to generate the code and run it.

Here is the API yaml

asyncapi: '2.3.0'
info:
  title: demo
  version: '1.0.0'
  description: websocket demo

servers:
  test:
    url: localhost
    protocol: ws

channels:
  data:
    publish:
      operationId: publishData
      message:
        $ref: '#/components/messages/dataMessage'
    subscribe:
      operationId: subscribeData
      message:
        $ref: '#/components/messages/dataMessage'

components:
  messages:
    dataMessage:
      headers:
        $ref: "#/components/schemas/MessageHeader"
      payload:
        oneOf:
          - $ref: "#/components/schemas/FirstPayloadType"
          - $ref: "#/components/schemas/SecondPayloadType"

  schemas:
    MessageHeader:
      type: object
      properties:
        messageType:
          enum:
            - a
            - b
            - c

    FirstPayloadType:
      type: object
      properties:
        x:
          type: string
        y:
          type: number

    SecondPayloadType:
      type: object
      properties:
        op:
          type: string
        tar:
          type: string

Then i use https://github.com/asyncapi/generator to generate the java code from this yaml with

ag asyncapi.yaml @asyncapi/java-spring-template

And thats where Im stuck, it uses springframework messaging and i dont know how to configure/implement it while using the generated message handler. Or maybe im missing something in the yaml so it doesnt generate properly.


Solution

  • That's because the template does not support anything other than Kafka, AMQP, and MQTT protocols.

    As of this writing, there does not exist any template for you to generate Java code for the WebSocket protocol.

    If you want the template to support the protocol, please create a feature request for it.

    Or you could author your own template if you'd like.