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

Add BBE for websocket query param #5636

Merged
merged 5 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -2827,6 +2827,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();
}

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
MohamedSabthar marked this conversation as resolved.
Show resolved Hide resolved

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
Loading