diogob / postgres-websockets

PostgreSQL + Websockets
https://hackage.haskell.org/package/postgres-websockets
MIT License
355 stars 28 forks source link

Feature request: disable or customize websocket response message #51

Closed W1M0R closed 4 years ago

W1M0R commented 4 years ago

When I send a websocket message from the browser to postgres-websockets, the websocket responds with an echo of what I sent, including the payload and additional properties such as message_delivered_at. If big payloads are sent, then having them echoed in response can be detrimental to performance.

Having the websocket echo an acknowledgement is great, but can we have the option to disable the acknowledgement, or to redact the payload (and potentially the claims) from it? This feature may be implemented by providing additional claims in the JWT that will be honoured by postgres-websockets, e.g. echoPayload: false, etc.

If you find the JWT approach good and expect to implement other features in a similar way, then it might be a good idea to put the claims in a JWT key that serves as a namespace for all future postgres-websockets claims, e.g.

{
-- private claims (e.g. as expected now)
"mode": "rw",
"channel": "chat",
-- public claims (i.e. good practice to avoid collisions and to encourage re-use of tokens)
"http://pgwebsockets.com/jwt/claims": {
  "x-pgwebsockets-echo": false,
  "x-pgwebsockets-mode": "rw",
  "x-pgwebsockets-channel": "chat",
}
}

Alternatively, it could be a global configuration setting for postgres-websockets. I'm note sure how pg-recorder is able to respond to messages sent to postgres-websockets, but I suspect it relies on the fact that the payload is echoed to connected clients.

diogob commented 4 years ago

@W1M0R what would be the difference between disabling echo and opening a channel in mode w ?

The echo is just a consequence of listening to the same broadcast channel we are writing to, we would have to write additional code to filter out messages issued by a particular client. If you could solve your case by opening a write only channel for the messages you don't want the echo and another channel to listen to incoming messages from the database that might be a better approach.

W1M0R commented 4 years ago

Your suggested approach makes sense and will sufficiently solve my use case. Now I understand the purpose of the mode field. Thanks.