WebSocket
This library enables you to create WebSockets using Wing.
WebSockets offer a persistent, bidirectional communication channel between a client and a server, facilitating real-time, low-latency communication.
By incorporating WebSockets through the Wing library, developers can enhance the interactivity and responsiveness of their applications, delivering a more engaging user experience. Whether you're building a real-time chat application or a collaborative tool, understanding and implementing WebSockets with Wing can significantly elevate your web development projects.
Prerequisites
Installation
npm i @winglibs/websockets
Usage
The example above shows us how we can broadcast a message to any connection assigned to the WebSocket.
bring cloud;
bring ex;
bring websockets;
let tb = new ex.DynamodbTable(
name: "WebSocketTable",
hashKey: "connectionId",
attributeDefinitions: {
"connectionId": "S",
},
);
let wb = new websockets.WebSocket(name: "MyWebSocket") as "my-websocket";
wb.onConnect(inflight(id: str): void => {
tb.putItem({
item: {
"connectionId": id
}
});
});
wb.onDisconnect(inflight(id: str): void => {
tb.deleteItem({
key: {
"connectionId": id
}
});
});
wb.onMessage(inflight (id: str, body: str): void => {
let connections = tb.scan();
for item in connections.items {
wb.sendMessage(str.fromJson(item["connectionId"]), body);
}
});
sim
When executing in the Wing Simulator, the WebSocket uses the ws
library.
tf-aws
When running on AWS, the WebSocket utilizes the WebSocket API of API Gateway.
awscdk
When running on AWS, the WebSocket utilizes the WebSocket API of API Gateway.
To compile to awscdk
, we need to import the @winglang/platform-awscdk
.
Maintainers
License
This library is licensed under the MIT License.
API Reference
Table of Contents
- Classes
- Interfaces
- Structs
WebSocket (preflight class)
No description
Constructor
new(props: WebSocketProps): WebSocket
Properties
Name | Type | Description |
---|---|---|
url | str | No description |
Methods
Signature | Description |
---|---|
onConnect(handler: inflight (str): void): void | No description |
onDisconnect(handler: inflight (str): void): void | No description |
onMessage(handler: inflight (str, str): void): void | No description |
inflight sendMessage(connectionId: str, message: str): void | No description |
platform.WebSocket_tfaws (preflight class)
No description
Constructor
new(props: WebSocketProps): WebSocket_tfaws
Properties
Name | Type | Description |
---|---|---|
url | str | No description |
Methods
Signature | Description |
---|---|
addRoute(handler: Function, routeKey: str): void | No description |
onConnect(handler: inflight (str): void): void | No description |
onDisconnect(handler: inflight (str): void): void | No description |
onMessage(handler: inflight (str, str): void): void | No description |
inflight sendMessage(connectionId: str, message: str): void | No description |
platform.WebSocket_sim (preflight class)
No description
Constructor
new(props: WebSocketProps): WebSocket_sim
Properties
Name | Type | Description |
---|---|---|
url | str | No description |
Methods
Signature | Description |
---|---|
onConnect(handler: inflight (str): void): void | No description |
onDisconnect(handler: inflight (str): void): void | No description |
onMessage(handler: inflight (str, str): void): void | No description |
inflight sendMessage(connectionId: str, message: str): void | No description |
platform.WebSocket_awscdk (preflight class)
No description
Constructor
new(props: WebSocketProps): WebSocket_awscdk
Properties
Name | Type | Description |
---|---|---|
url | str | No description |
Methods
Signature | Description |
---|---|
addRoute(handler: Function, routeKey: str): void | No description |
onConnect(handler: inflight (str): void): void | No description |
onDisconnect(handler: inflight (str): void): void | No description |
onMessage(handler: inflight (str, str): void): void | No description |
inflight sendMessage(connectionId: str, message: str): void | No description |
platform.aws.IAwsWebSocket (interface)
No description
Properties
Name | Type | Description |
---|---|---|
node | Node | The tree node. |
Methods
Signature | Description |
---|---|
addRoute(handler: Function, routeKey: str): void | No description |
onConnect(handler: inflight (str): void): void | No description |
onDisconnect(handler: inflight (str): void): void | No description |
onMessage(handler: inflight (str, str): void): void | No description |
inflight sendMessage(connectionId: str, message: str): void | No description |
commons.IWebSocket (interface)
No description
Properties
Name | Type | Description |
---|---|---|
node | Node | The tree node. |
Methods
Signature | Description |
---|---|
onConnect(handler: inflight (str): void): void | No description |
onDisconnect(handler: inflight (str): void): void | No description |
onMessage(handler: inflight (str, str): void): void | No description |
inflight sendMessage(connectionId: str, message: str): void | No description |
platform.aws.WebSocketAwsRequest (struct)
No description
Properties
Name | Type | Description |
---|---|---|
body | str | No description |
requestContext | WebSocketAwsRequestContext | No description |
platform.aws.WebSocketAwsRequestContext (struct)
No description
Properties
Name | Type | Description |
---|---|---|
connectionId | str | No description |
routeKey | str | No description |
platform.aws.WebSocketAwsResponse (struct)
No description
Properties
Name | Type | Description |
---|---|---|
body | str? | No description |
statusCode | num | No description |
commons.WebSocketProps (struct)
No description
Properties
Name | Type | Description |
---|---|---|
name | str | No description |
stageName | str? | No description |