@@ -8,6 +8,7 @@ const socketcluster = require('socketcluster-client')
88// Public channel idenitfier and general options
99let options = { waitForAuth : true }
1010let userChannel
11+ let publicChannel
1112let session = reactive ( { user : { } } )
1213
1314// Initiate the connection to the websocket server
@@ -39,6 +40,12 @@ export const socketLogout = socketUser => {
3940 socket . emit ( 'loggedOut' )
4041}
4142
43+ export const watchPublicChannel = handler => {
44+ if ( window . websocket_logs ) console . log ( 'Watching public channel.' )
45+ if ( publicChannel ) publicChannel . watch ( handler )
46+ else setTimeout ( ( ) => watchPublicChannel ( handler ) , 1000 )
47+ }
48+
4249export const watchUserChannel = handler => {
4350 if ( window . websocket_logs ) console . log ( 'Watching user channel.' )
4451 if ( userChannel ) userChannel . watch ( handler )
@@ -87,7 +94,7 @@ export default {
8794 } )
8895 }
8996 else if ( JSON . parse ( channelName ) . type === 'public' ) {
90- socket . watch ( channelName , d => d . action === 'announcement' ? alertStore . warn ( d . message ) : null )
97+ // Placeholder for future public notifications if necessary
9198 }
9299 else window . websocket_logs ? console . log ( 'Not watching' , channelName ) : null
93100
@@ -125,14 +132,15 @@ export default {
125132 socket . on ( 'connect' , status => status . isAuthenticated ? socket . emit ( 'loggedIn' ) : null )
126133
127134 // always subscribe to the public channel
128- socket . subscribe ( publicChannelKey , options )
135+ publicChannel = socket . subscribe ( publicChannelKey , { waitForAuth : false } )
129136
130137 /* Provide Store Data */
131138 return provide ( WebsocketService , {
132139 socketLogin,
133140 socketLogout,
134141 watchUserChannel,
135142 unwatchUserChannel,
143+ watchPublicChannel,
136144 isOnline,
137145 } )
138146 } ,
0 commit comments