Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create WebSocket example #253

Closed
vit1251 opened this issue Aug 5, 2019 · 20 comments
Closed

Create WebSocket example #253

vit1251 opened this issue Aug 5, 2019 · 20 comments
Labels
keep-open Prevents stale bot from closing this issue or PR

Comments

@vit1251
Copy link

vit1251 commented Aug 5, 2019

Could you please provide WebSocket AsyncAPI 2.0.0 complete example.

Right now I totally misunderstand how to describe single channel (i.e. I use only one path "/ws" for example) RPC service with messages like SignUpRequest, SignUpResponse, etc.

@treeder
Copy link

treeder commented Aug 8, 2019

I had the same question while working on this demo application: https://github.com/treeder/async-toy-store

I ended up wrapping the messages in metadata that included the channel name: https://github.com/treeder/async-toy-store/blob/master/models/message.go

Not sure how to describe that in AsyncAPI though.

@fmvilas
Copy link
Member

fmvilas commented Aug 29, 2019

Hey folks! I just created a code generator for Node.js and WebSockets. With it, I created an example of an echo client/server using WebSockets. Check it out: https://github.com/fmvilas/asyncapi-websockets-example.

The AsyncAPI file is this one: https://github.com/fmvilas/asyncapi-websockets-example/blob/master/asyncapi.yaml.

Hope it helps!

@stale
Copy link

stale bot commented Oct 29, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Oct 29, 2019
@fmvilas fmvilas removed the stale label Oct 30, 2019
@stale
Copy link

stale bot commented Dec 29, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Dec 29, 2019
@stale stale bot closed this as completed Jan 5, 2020
@derberg derberg removed the stale label Mar 10, 2020
@derberg
Copy link
Member

derberg commented Mar 10, 2020

It is still needed to have WS example among other examples basing on this thread https://asyncapi.slack.com/archives/C34F2JV0U/p1583757080132600, not to mention that it would make sense to have a documentation that explains the example

@derberg derberg reopened this Mar 10, 2020
@fmvilas fmvilas added this to the Plan and design high-quality documentation. milestone Mar 13, 2020
@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity 😴
It will be closed in 30 days if no further activity occurs. To unstale this issue, add a comment with detailed explanation.
Thank you for your contributions ❤️

@github-actions github-actions bot added the stale label May 13, 2020
@derberg derberg removed the stale label Jun 15, 2020
@derberg derberg reopened this Jun 15, 2020
@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity 😴
It will be closed in 30 days if no further activity occurs. To unstale this issue, add a comment with detailed explanation.
Thank you for your contributions ❤️

@github-actions github-actions bot added the stale label Aug 15, 2020
@fmvilas fmvilas added keep-open Prevents stale bot from closing this issue or PR and removed stale labels Aug 15, 2020
@derberg
Copy link
Member

derberg commented Nov 13, 2020

I think that this could be my next “assignment”. I’d love to do as much as possible here:

What do you think should be my focus. What would you like to see there, what should I showcase? What would you love to get as a user? 🤔

@fmvilas
Copy link
Member

fmvilas commented Nov 13, 2020

I'd make sure the project showcases:

  1. How to define a WebSockets API with AsyncAPI.
    1. With multiple channels.
    2. With a single channel but multiple messages.
  2. Generate code for multi-channel Websockets API with Socket.io.
  3. Generate code for single-channel multiple-messages WebSockets API with raw WebSockets.
  4. Generate code for both server and client.

@jonaslagoni
Copy link
Member

Would love to see an example for internal communication in terms of Node.js + Socket.io server and client 🙇

@lbdremy
Copy link

lbdremy commented Feb 8, 2021

Nice initiative really fill a hole in the OpenAPI Spec!

I'm trying to model a WS API with the AsyncAPI Spec, I have PUBLISH, SUBSCRIBE but also UNSUBSCRIBE operations, where do you put these operations inside the PUBLISH section? Or am I missing unsubscribe somewhere (https://www.asyncapi.com/docs/specifications/2.0.0#channelItemObject)?

Also, I read your article @fmvilas about organizing your AsyncAPI documents I understood you recommend doing two AsyncAPI documents, one for the client perspective and one from the server perspective. Do you have an example available with the two documents?

I understand in more complex systems with a lot of different consumers/producers it may be interesting to have multiple documents, still, a document to explain the whole system is nice to have. In a WS scenario, what is missing to achieve this, is a field to indicate who can subscribe, and who publishes a message, with WS in mind since there are only two members is easy to identify the member at the other end.

Thanks for your help

@fmvilas
Copy link
Member

fmvilas commented Feb 9, 2021

Hey @lbdremy!

I'm trying to model a WS API with the AsyncAPI Spec, I have PUBLISH, SUBSCRIBE but also UNSUBSCRIBE operations, where do you put these operations inside the PUBLISH section? Or am I missing unsubscribe somewhere (https://www.asyncapi.com/docs/specifications/2.0.0#channelItemObject)?

Yeah, we don't consider unsubscribe as an operation in the spec. An unsubscribe operation is actually a publish to a specific channel with a specific message. It's not the first time someone asks for an unsubscribe operation. This is something we'll have to consider for future spec versions along with request and reply.

Now regarding the file organization, I think in the case of a client/server WebSockets connection, it doesn't make sense to have multiple AsyncAPI files. You could just have one as you do with OpenAPI for instance. The AsyncAPI file represents the server and the publish and subscribe operations represent what your clients can send to you or receive from you, respectively.

The difference with a broker architecture here is that this pattern —even though it's messaging— is not broker-based but instead it follows the "traditional" client/server architecture and the connection is connecting both the client and the server directly, as in an HTTP API. That's why in these cases, I think it makes sense to have only one file for the server and the client just takes it for reference.

Hope that helps :)

@lbdremy
Copy link

lbdremy commented Feb 9, 2021

Yes it helps :) thanks @fmvilas

Now regarding the file organization, I think in the case of a client/server WebSockets connection, it doesn't make sense to have multiple AsyncAPI files. You could just have one as you do with OpenAPI for instance. The AsyncAPI file represents the server and the publish and subscribe operations represent what your clients can send to you or receive from you, respectively.

I agree one OpenAPI file to describe is much better still, in my case, the publish and subscribe operations do not only represent what my clients can send to you or receive from you, respectively. But also the client can publishmessage on a channel for example in a chat room, also a server can subscribe to a channel of the app representing some activities of the app for example "mouse tracking" less common I agree. That's the reason why I was suggesting to have a field to indicate who can subscribe, and who publishes a message. Today I am going to use the summary field and the tags field to indicate this information, it is free text so it is possible but not formalized.

Thanks again for your help :)

@fmvilas
Copy link
Member

fmvilas commented Feb 9, 2021

But also the client can publish message on a channel for example in a chat room

This is the meaning of publish in the server. It means that a client (of that server) can publish a message. It doesn't mean that the server is publishing a message to the client.

also a server can subscribe to a channel of the app representing some activities of the app for example "mouse tracking" less common I agree

Who is "the app" here? Is it the client? If it's the client this case is still a publish. It's the same to say that a server can subscribe to a channel (so the client sends a message there) than saying that a client can publish a message to a channel. Does it make sense?

Usually, the trick here is to think about the communication always from the client perspective. You define what the client can/will do. Therefore, saying that a client can subscribe means that the server is going to publish. Otherwise, why is the client subscribing, right? 😄

@alex-mueller
Copy link

Hi,

regarding the initial question I would like to know if I get it right based on the following information:
https://github.com/asyncapi/bindings/blob/master/websockets/README.md

When using WebSockets, the channel represents the connection

That means when I'm trying to model an API between two micro services that is based on Websockets, I essentially ending up defining a single Channel where publish/subscribe probably contain each an anyOf message type, correct?
Should I try to model it the way how @treeder did in his example by adding the channel (or path) to the message payload?
I know, enhancing the docu is still pending but I hope I get at least some feelings about this from you. And sorry if the answer is in the messages above, I read them multiple times and the links but I still had this question ^^

Thanks

@derberg
Copy link
Member

derberg commented Apr 21, 2021

I wrote two articles about describing WebSockets with AsyncAPI that should explain the relationship between the two and how to use AsyncAPI to describe WebSocket API. It is based on real WebSocket API (single channel) as I could not come up with a better artificial example. Life is better in creating different scenarios.

I want to also record a live stream about the above, to basically provide the same content in audio and video.

I plan one article more + a presentation on a conference. The plan is to write a super basic file (multi-channel) and do some code generation with it. I was thinking about writing a new template from scratch using socket.io (as suggested above) but after playing with https://github.com/asyncapi/nodejs-ws-template I think it is a good and very simple template suitable for education. Let's see where it goes.

Let me know what you think.

If you know a technical writer or just anyone good at docs, that has time to contribute to open-source, please connect us. Would be great if someone can help, grab my content, do some Q&A sessions and create official documentation that we could host on our AsyncAPI website 🙏🏼

@derberg
Copy link
Member

derberg commented Apr 30, 2021

@fmvilas fmvilas removed this from the Plan and design high-quality documentation. milestone May 12, 2021
@derberg
Copy link
Member

derberg commented May 19, 2021

Last blog post is out https://www.asyncapi.com/blog/websocket-part3

Let me close this issue and please let me know in a new issue if something more is needed

@derberg derberg closed this as completed May 19, 2021
@Jaikant
Copy link

Jaikant commented Feb 8, 2022

@fmvilas We have a microservices based internal pub-sub architecture, and we have webhooks as interfaces to third party applications. I was looking to use AsyncApi as my single source of truth for my internal and external APIs. Looks like that isn't possible yet.

@fmvilas
Copy link
Member

fmvilas commented Feb 14, 2022

@Jaikant Yeah, still not possible. Would be awesome if you create an issue to request for this functionality and maybe even champion it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
keep-open Prevents stale bot from closing this issue or PR
Projects
None yet
Development

No branches or pull requests

8 participants