question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebSocket: Publish message to clients

See original GitHub issue

Continuation 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:open
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
ceilforscommented, Jun 26, 2019

This diagram from AWS should give a high level overview:

image

0reactions
hugosenaricommented, Feb 26, 2020

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. 🤔

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found