DEV Community

Discussion on: Are there any API specification formats for WebSockets?

Collapse
 
derberg profile image
Lukasz Gornicki

Definitely AsyncAPI -> github.com/asyncapi/asyncapi/

We have many people asking about websockets in our slack asyncapi.com/slack-invite/

Collapse
 
idanarye profile image
Idan Arye

Still seems thin in the language support department - only thing added since I posted this almost a year ago is node.js templates.

I did find this ticket though - github.com/asyncapi/generator/issu... - which suggests using something like quicktype.io/. So maybe I should just use quicktype?

Collapse
 
derberg profile image
Lukasz Gornicki

Last year was fully focused on releasing AsyncAPI 2.0, so getting the spec more mature.
This year we focus on tooling. Come and join us.

Quicktype will generate just types for you, this is why we want to integrate it in generator for code templates. But it cannot really replace generator.

What is that you exactly need except of the spec? What do you want to get out of the spec?

Thread Thread
 
idanarye profile image
Idan Arye

I need the specs to describe the methods that can be sent. Each method needs to describe:

  1. Its name.
  2. Its fields (and their possibly deep types)
  3. What it returns.
  4. Free text to describe what it does.

Ideally I'd like something that can generate the full API. For example, if I have

- name: foo
  parameters:
    - name: bar
      type: integer
    - name: baz
      type: text
  returns:
    - name: x
      type: float
    - name: y
      type: float
  doc: bla bla bla

It would generate, say for Java:

public class FooResult {
    private float x;
    private float y;

    public FooResult(float x, float y) {
        this.x = x;
        this.y = y;
    }

    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }
}

public abstract class AbstractAPI {
    protected abstract Value call(String method, Value parameters);

    /**
     * bla bla bla
     */
    public FooResult foo(int bar, String baz) {
        Value request = new Value();
        request.setInt("bar", bar);
        request.setString("baz", baz);

        Value result = call("foo", request);

        return FooResult(result.getFloat("x"), result.getFloat("y"));
    }
}

And then you'll just have to subclass AbstractAPI (yes, I know) and implement call and you'll get fully typed access to the entire API.

Thread Thread
 
derberg profile image
Lukasz Gornicki

Hey, fyi I published some materials about WebSocket API and AsyncAPI github.com/asyncapi/spec/issues/25...

also, for code generation -> we released a new library for generating types/models. It can be used with our generator and also as a standalone. Long term we want to integrate it with new CLI so you can easily generate models from CLI -> github.com/asyncapi/generator-mode...