File tree Expand file tree Collapse file tree 2 files changed +19
-3
lines changed
Expand file tree Collapse file tree 2 files changed +19
-3
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments