WebSocket: Publish message to clients
See original GitHub issueContinuation of the discussion that we have at #41
As using combining DynamoDB and WebSocket is a common use case, laconia can make users life easier by publishing the below public contracts.
Discussions
- Is broadcasting a common scenario?
- If WebSocket is used to reply to individual clients, how do we know which client do we want to retrieve? It looks like there’s an additional info from the user land that we need to store if we want to support this
On connect / disconnect
const { websocketHandler } = require('@laconia/websocket')
// Understands event type of $connect and $disconnect, automatically store and delete connection ID in DynamoDB
exports.handler = websocketHandler('WEBSOCKET_CONNECTION_TABLE_NAME')
On sending message API is inspired from: https://github.com/websockets/ws
const { createWss } = require("@laconia/websocket");
// wss object is injected
const app = async (event, { wss }) => {
// Automatically get all client objects from DynamoDB
const clients = await wss.getClients()
// Sends data to a client, wraps apigwManagementApi
// What's the best way to find the client here?
await clients[0].send("something")
// Broadcast, also wraps apigwManagementApi
await Promise.all(clients.map(c => c.send('something')))
await wss.broadcast('something')
};
exports.handler = laconia(app).register(createWss('WEBSOCKET_CONNECTION_TABLE_NAME'));
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:9 (7 by maintainers)
Top Results From Across the Web
Sending message to a specific connected users using ...
Sending message to a specific connected users using webSocket? · When the page finishes loading (DOM ready) - establish a connection to the...
Read more >Publish and subscribe messages between WebSocket clients ...
Tutorial: Publish and subscribe messages between WebSocket clients using subprotocol · In this article · Prerequisites · Create an Azure Web PubSub ...
Read more >Spring WebSockets: Send Messages to a Specific User
In this tutorial, we'll describe how to use Spring WebSockets to send STOMP messages to a single user. That's important because we sometimes ......
Read more >Writing WebSocket client applications - Web APIs | MDN
WebSocket client applications use the WebSocket API to communicate with WebSocket servers using the WebSocket protocol.
Read more >Broadcasting messages - websockets 10.4 documentation
If you just want to send a message to all connected clients, use broadcast() . If you want to learn about its design...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This diagram from AWS should give a high level overview:
We are not dependant of serverless.js but is currently most easily way to deploy lambda and serverless/components have two interesting components PubSub (push message with websocket isn’t too different) and chat-app (that is what we tried to achieve) we could mashup both.
off-topic: maybe we could abstract some other components. 🤔