Skip to content

Commit 37b653e

Browse files
committed
fix: don't automatically subscribe user's to any topic
1 parent 0de5515 commit 37b653e

File tree

2 files changed

+19
-3
lines changed
  • playground/server/routes
  • src/runtime/server/utils

2 files changed

+19
-3
lines changed

playground/server/routes/_ws.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ export default defineWSHandler({
2828
},
2929

3030
async message(peer, message) {
31+
const mem = useMem('ws')
32+
// Validate incoming subscription/unsubscription
33+
const parsedSubUnsub = await wsSafeValidateMessage(
34+
v.object({
35+
type: v.picklist(['subscribe', 'unsubscribe']),
36+
topic: v.string(),
37+
}),
38+
message,
39+
)
40+
if (!parsedSubUnsub.issues && parsedSubUnsub.value.type === 'subscribe') {
41+
// Subscribe to the topic
42+
peer.subscribe(parsedSubUnsub.value.topic)
43+
// Fetch data from storage and send it to the peer
44+
const payload = await mem.getItem(parsedSubUnsub.value.topic)
45+
if (payload)
46+
peer.send(JSON.stringify({ topic: parsedSubUnsub.value.topic, payload }), { compress: true })
47+
}
48+
3149
// Validate the incoming message
3250
const parsedMessage = await wsValidateMessage(
3351
v.union([
@@ -48,7 +66,6 @@ export default defineWSHandler({
4866
]),
4967
message,
5068
)
51-
const mem = useMem('ws')
5269

5370
if (parsedMessage.type === 'publish') {
5471
const { topic, payload } = parsedMessage

src/runtime/server/utils/ws.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ export function defineWSHandler(hooks: Partial<WSHandlerHooks>) {
5959
const config = getConfig()
6060
const m = wsParseMessage(message.text())
6161
if (m?.topic && config.topics.internals.includes(m.topic)) return
62-
if (m?.type === 'subscribe' && m?.topic && !config.topics.defaults.includes(m.topic))
63-
peer.subscribe(m.topic)
62+
if (m?.type === 'subscribe' && m?.topic && config.topics.defaults.includes(m.topic)) return
6463
if (m?.type === 'unsubscribe' && m?.topic)
6564
peer.unsubscribe(m.topic)
6665

0 commit comments

Comments
 (0)