Skip to content

Commit

Permalink
Address review suggessions
Browse files Browse the repository at this point in the history
  • Loading branch information
hasathcharu committed Jul 1, 2024
1 parent 3788e3b commit 605bffe
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 14 deletions.
6 changes: 4 additions & 2 deletions samples/basic-chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ This is a set of Ballerina WebSocket test examples. The examples demonstrates ho
You can generate a demo AsyncAPI specification using the `server-demo` package. A pregenerated specification already exists in the `specs` directory. If you want to generate the specification again, you can use the following command, where `main.bal` is the Ballerina file that contains the entry point to the WebSocket service.

```bash
cd server-demo
bal asyncapi --protocol ws -i main.bal -o specs
```

## A client-server example

The `client` package and the `server` package are a simple example of a client-server application. The client sends a message to the server, which responds with a message, and the client logs the response. The client continuously listens for messages from the server.
The `client` and `server` packages exemplify a basic client-server application. The client sends a message to the server, which responds with a message, and the client logs the response. The client continuously listens for messages from the server.

You can have two instances of the client to pass messages between them through the server.

In the `client` package, the files `client.bal`, `types.bal`, and `utils.bal` are generated by the AsyncAPI tool, whereas the `main.bal` file is an example usage of the generated client.

In the `server` package, the `messages.md` file contains the message examples that the server expects. The `main.bal` file is the WebSocket server implementation.
In the `server` package, the `messages.md` file provides example messages outlining the expected structure by the server. The `main.bal` file contains the WebSocket server implementation.

You can regenerate the client using the following command:

```bash
cd client
bal asyncapi --protocol ws -i asyncapi.yaml
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,4 @@ components:
$ref: '#/components/messages/Response'
x-response-type: simple-rpc
x-dispatcherKey: event
x-dispatcherStreamId: id
x-dispatcherStreamId: id
4 changes: 1 addition & 3 deletions samples/basic-chat/client-with-dispatcherStreamId/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ public function main() returns error? {

function printSingleResponse(stream<Response,error?> subscription) {
record {|Response value;|}|error? message = subscription.next();
if message !is error? {
if message is record {|Response value;|} {
io:println(message.value);
} else if message is error {
io:println("Error occurred at worker: " + message.message());
} else {
io:println("NILL");
}
}
6 changes: 2 additions & 4 deletions samples/basic-chat/client/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ public function main() returns error? {

function printSingleResponse(stream<Response,error?> subscription) {
record {|Response value;|}|error? message = subscription.next();
if message !is error? {
if message is record {|Response value;|} {
io:println(message.value);
} else if message is error {
io:println("Error occurred at worker: " + message.message());
} else {
io:println("NILL");
}
}
}
2 changes: 1 addition & 1 deletion samples/basic-chat/server-demo/messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
## Chat Message Example

```json
{"event":"chat","message":"Hello ballerina", "toUserId": "cc6b1efffe42ea75-0001e3b1-00000002-237f98c8d2877f67-d7aa765b"}
{"event":"chat","message":"Hello Ballerina", "toUserId": "cc6b1efffe42ea75-0001e3b1-00000002-237f98c8d2877f67-d7aa765b"}
```
4 changes: 2 additions & 2 deletions samples/basic-chat/server-with-dispatcherStreamId/messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
## Unsubscribe Message Example

```json
{"event":"unsubscribe", id:"Hellooooo"}
{"event":"unsubscribe", id:"UNSUB_ID"}
```

## Chat Message Example

```json
{"event":"chat","message":"Hello ballerina", "toUserId": "cc6b1efffe42ea75-000420c8-00000002-3ff441ce1aa91480-ef8175ca", "id":"CHAT_ID"}
{"event":"chat","message":"Hello Ballerina", "toUserId": "cc6b1efffe42ea75-000420c8-00000002-3ff441ce1aa91480-ef8175ca", "id":"CHAT_ID"}
```
2 changes: 1 addition & 1 deletion samples/basic-chat/server/messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
## Chat Message Example

```json
{"event":"chat","message":"Hello ballerina", "toUserId": "cc6b1efffe42ea75-0003c7e2-00000002-aa19eabfbcf62d1d-48179b7d"}
{"event":"chat","message":"Hello Ballerina", "toUserId": "cc6b1efffe42ea75-0003c7e2-00000002-aa19eabfbcf62d1d-48179b7d"}
```

0 comments on commit 605bffe

Please sign in to comment.