Skip to content

Commit

Permalink
Merge pull request #5636 from MohamedSabthar/master
Browse files Browse the repository at this point in the history
Add BBE for websocket query param
  • Loading branch information
NipunaRanasinghe authored Oct 1, 2024
2 parents f8b18d9 + 3a7cc7a commit 6a1b06f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -2856,6 +2856,15 @@
"disableVerificationReason": "Service cannot be stopped",
"disablePlayground": true,
"isLearnByExample": false
},
{
"name": "Query parameter",
"url": "websocket-query-parameter",
"verifyBuild": true,
"verifyOutput": false,
"disableVerificationReason": "Service cannot be stopped",
"disablePlayground": true,
"isLearnByExample": false
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ function testText() returns websocket:Error? {
check wsClient->writeMessage(msg);
string serviceReply = check wsClient->readMessage();
test:assertEquals(serviceReply, "Hello!, How are you?");
check wsClient->close();
}

25 changes: 25 additions & 0 deletions examples/websocket-query-parameter/websocket_query_parameter.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import ballerina/websocket;

service /chat on new websocket:Listener(9090) {

// The `userName` parameter in the resource method is treated as a query parameter,
// which is extracted from the request URI. For example, invoking this service with
// the URI `ws://localhost:9090/chat?userName=John` passes `John` as the value
// to the `userName` parameter defined in the resource method.
resource function get .(string userName) returns websocket:Service {
return new ChatService(userName);
}
}

service class ChatService {
*websocket:Service;
private final string userName;

function init(string userName) {
self.userName = userName;
}

remote function onOpen(websocket:Caller caller) returns error? {
check caller->writeMessage(string `Welcome ${self.userName}!`);
}
}
16 changes: 16 additions & 0 deletions examples/websocket-query-parameter/websocket_query_parameter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# WebSocket service - Query parameter

The parameter in the `get` resource method represents the query segment of the request URL. The parameter name should match the query key, and its value is mapped at runtime by extracting it from the URL. Supported types for query parameters include `string`, `int`, `float`, `boolean`, and `decimal`. Query parameter types can be nilable (e.g., `string? bar`). When a request contains query segments, retrieving them as resource arguments is simpler and highly recommended. Alternatively, the `http:Request` object can be used as the parameter of the `get` resource method, which also exposes methods to retrieve query parameters as well.

::: code websocket_query_parameter.bal :::

Run the service as follows.

::: out websocket_query_parameter.server.out :::

>**Tip:** You can invoke the above service via the [WebSocket client](/learn/by-example/websocket-client/).

## Related links
- [`websocket` module - API documentation](https://lib.ballerina.io/ballerina/websocket/latest)
- [WebSocket service - Specification](/spec/websocket/#31-upgrade-service)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
description: This example is on how to extract the query parameters from websocket URL.
keywords: ballerina, ballerina by example, bbe, websocket service, query parameter
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ bal run websocket_query_parameter.bal

0 comments on commit 6a1b06f

Please sign in to comment.