0.3.7 • Published 18 days ago

@winglibs/websockets v0.3.7

Weekly downloads
-
License
MIT
Repository
github
Last release
18 days ago

Websockets

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

@marciocadev

License

This library is licensed under the MIT License.

0.3.7

18 days ago

0.3.6

28 days ago

0.3.5

1 month ago

0.3.4

2 months ago

0.3.3

2 months ago

0.3.0

2 months ago

0.3.2

2 months ago

0.3.1

2 months ago

0.2.3

2 months ago

0.2.1

3 months ago

0.2.0

3 months ago

0.2.2

3 months ago

0.1.2

3 months ago

0.1.1

4 months ago

0.1.0

4 months ago

0.0.2

5 months ago

0.0.1

5 months ago