-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5636 from MohamedSabthar/master
Add BBE for websocket query param
- Loading branch information
Showing
6 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
examples/websocket-query-parameter/websocket_query_parameter.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
examples/websocket-query-parameter/websocket_query_parameter.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
2 changes: 2 additions & 0 deletions
2
examples/websocket-query-parameter/websocket_query_parameter.metatags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
1 change: 1 addition & 0 deletions
1
examples/websocket-query-parameter/websocket_query_parameter.server.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
$ bal run websocket_query_parameter.bal |