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

docs: small social media system example #651

Merged
merged 14 commits into from
Dec 21, 2021
46 changes: 46 additions & 0 deletions examples/social-media/backend.asyncapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
asyncapi: 2.2.0

info:
title: Website Backend
version: 1.0.0

servers:
websiteWebSocketServer:
url: ws://mycompany.com/ws
protocol: ws
mosquitto:
url: mqtt://test.mosquitto.org
protocol: mqtt
bindings:
mqtt:
clientId: websocketServer

channels:
comment/liked:
description: Notify all the services that a comment has been liked.
servers: ['mosquitto']
subscribe:
message:
$ref: './dictionary.asyncapi.yaml#/components/messages/commentLiked'
like/comment:
description: When a comment like is received from the frontend.
servers: ['websiteWebSocketServer']
publish:
message:
$ref: './dictionary.asyncapi.yaml#/components/messages/likeComment'
comment/{commentId}/changed:
description: When an event from the broker arrives telling us to update the comment likes count on the frontend.
parameters:
commentId:
schema:
$ref: './dictionary.asyncapi.yaml#/components/schemas/commentId'
servers: ['mosquitto']
publish:
message:
$ref: './dictionary.asyncapi.yaml#/components/messages/commentChanged'
update/comment/likes:
description: Update comment likes count in the frontend.
servers: ['websiteWebSocketServer']
subscribe:
message:
$ref: './dictionary.asyncapi.yaml#/components/messages/updateCommentLikes'
31 changes: 31 additions & 0 deletions examples/social-media/comments-service.asyncapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
asyncapi: 2.2.0

info:
title: Comments Service
version: 1.0.0
description: This service is in charge of processing all the events related to comments.

servers:
mosquitto:
url: mqtt://test.mosquitto.org
protocol: mqtt
bindings:
mqtt:
clientId: comment-service

channels:
comment/liked:
description: Updates the likes count in the database and sends the new count to the broker.
publish:
message:
$ref: './dictionary.asyncapi.yaml#/components/messages/commentLiked'
comment/{commentId}/changed:
description: Sends the new count to the broker after it has been updated in the database.
parameters:
commentId:
schema:
$ref: './dictionary.asyncapi.yaml#/components/schemas/commentId'
subscribe:
message:
$ref: './dictionary.asyncapi.yaml#/components/messages/commentChanged'

60 changes: 60 additions & 0 deletions examples/social-media/dictionary.asyncapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
asyncapi: 2.2.0

info:
title: Dictionary for organization-wide definitions
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
version: 1.0.0

components:
messages:
commentLiked:
payload:
$ref: '#/components/schemas/commentLikedPayload'
likeComment:
payload:
$ref: '#/components/schemas/likeCommentPayload'
commentChanged:
payload:
$ref: '#/components/schemas/commentChangedPayload'
updateCommentLikes:
payload:
$ref: '#/components/schemas/updateCommentLikesPayload'

schemas:
commentLikedPayload:
type: object
title: commentLikedPayload
additionalProperties: false
properties:
commentId:
allOf:
- $ref: '#/components/schemas/commentId'
- description: Id of the comment that was liked
likeCommentPayload:
type: object
title: likeCommentPayload
additionalProperties: false
properties:
commentId:
allOf:
- $ref: '#/components/schemas/commentId'
- description: Id of the comment that should be liked
commentChangedPayload:
type: object
title: commentChangedPayload
additionalProperties: false
properties:
commentId:
allOf:
- $ref: '#/components/schemas/commentId'
- description: Id of the comment that was changed, such as when someone liked it.
updateCommentLikesPayload:
type: object
title: updateCommentLikesPayload
additionalProperties: false
properties:
commentId:
allOf:
- $ref: '#/components/schemas/commentId'
- description: Id of the comment that was changed, such as when someone liked it.
commentId:
type: string
22 changes: 22 additions & 0 deletions examples/social-media/frontend.asyncapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
asyncapi: 2.2.0

info:
title: Website WebSocket Client
version: 1.0.0

servers:
websiteWebSocketServer:
url: ws://mycompany.com/ws
protocol: ws

channels:
like/comment:
description: Notify the backend that a comment has been liked.
subscribe:
message:
$ref: './dictionary.asyncapi.yaml#/components/messages/likeComment'
update/comment/likes:
description: Update the UI when the comment likes count is updated.
publish:
message:
$ref: './dictionary.asyncapi.yaml#/components/messages/updateCommentLikes'
20 changes: 20 additions & 0 deletions examples/social-media/notification-service.asyncapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
asyncapi: 2.2.0

info:
title: Notifications Service
version: 1.0.0

servers:
mosquitto:
url: mqtt://test.mosquitto.org
protocol: mqtt
bindings:
mqtt:
clientId: notification-service

channels:
comment/liked:
description: When a "comment has been liked" message is received, it sends an SMS or push notification to the author.
publish:
message:
$ref: './dictionary.asyncapi.yaml#/components/messages/commentLiked'
21 changes: 21 additions & 0 deletions examples/social-media/public.asyncapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
asyncapi: 2.2.0

info:
title: Public API
description: Public API for others to interact with the system
version: 1.0.0

servers:
mosquitto:
url: mqtt://test.mosquitto.org
protocol: mqtt
bindings:
mqtt:
clientId: public-api

channels:
comment/liked:
description: When a "comment has been liked" message is received, it sends an SMS or push notification to the author.
publish:
message:
$ref: './dictionary.asyncapi.yaml#/components/messages/commentLiked'
19 changes: 19 additions & 0 deletions examples/social-media/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## Small social media example

Say we have a social network, a very basic one, where all you can do is upvoting comments.

The individual services that need to be defined are:

- A frontend application where users interact through the website, where they can like comments.
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
- A backend WebSocket server that sends and receives events for the UI to update in real-time.
- A comment service which processes all events related to comments through a message broker.
- A public-facing API which allows others to get notified about updates.
Copy link
Member

@smoya smoya Nov 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not clear enough for me what this public facing API would do. Here you mention allows others to get notified about updates, however in the public.asyncapi.yaml file, there is only one operation which is publish. Shall we add a Subscribe Operation so users can be notified?

Copy link
Member Author

@jonaslagoni jonaslagoni Nov 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The public API needs to be seen as an external application, that anyone can create to interact with the system. We still use publish because that application needs to consume messages, because the internal publish to it 🙂


[![](https://mermaid.ink/img/eyJ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2ZXIyIC0tIFwiY29tbWVudC97Y29tbWVudElkfS9jaGFuZ2VkXCIgLS0-IFdlYnNpdGVCYWNrZW5kXG5QdWJsaWNBUElbUHVibGljIEFQSV1cbnNlcnZlcjIgLS0gXCJjb21tZW50L2xpa2VkXCIgLS0-IFB1YmxpY0FQSSIsIm1lcm1haWQiOiJ7XG4gIFwidGhlbWVcIjogXCJkYXJrXCJcbn0iLCJ1cGRhdGVFZGl0b3IiOmZhbHNlLCJhdXRvU3luYyI6dHJ1ZSwidXBkYXRlRGlhZ3JhbSI6ZmFsc2V9)

This ensures that we try to describe the following use-cases mixed together:

- A public API
- A WebSocket server and client
- A broker setup