1- import type { FastifyRequest } from 'fastify' ;
2- import superjson from 'superjson' ;
3-
41import type { WebSocket } from '@fastify/websocket' ;
5- import {
6- eventBuffer ,
7- getProfileById ,
8- transformMinimalEvent ,
9- } from '@openpanel/db' ;
2+ import { eventBuffer } from '@openpanel/db' ;
103import { setSuperJson } from '@openpanel/json' ;
11- import { subscribeToPublishedEvent } from '@openpanel/redis' ;
4+ import {
5+ psubscribeToPublishedEvent ,
6+ subscribeToPublishedEvent ,
7+ } from '@openpanel/redis' ;
128import { getProjectAccess } from '@openpanel/trpc' ;
139import { getOrganizationAccess } from '@openpanel/trpc/src/access' ;
10+ import type { FastifyRequest } from 'fastify' ;
1411
1512export function wsVisitors (
1613 socket : WebSocket ,
1714 req : FastifyRequest < {
1815 Params : {
1916 projectId : string ;
2017 } ;
21- } > ,
18+ } >
2219) {
2320 const { params } = req ;
24- const unsubscribe = subscribeToPublishedEvent ( 'events' , 'saved' , ( event ) => {
25- if ( event ?. projectId === params . projectId ) {
26- eventBuffer . getActiveVisitorCount ( params . projectId ) . then ( ( count ) => {
27- socket . send ( String ( count ) ) ;
28- } ) ;
21+ const sendCount = ( ) => {
22+ eventBuffer . getActiveVisitorCount ( params . projectId ) . then ( ( count ) => {
23+ socket . send ( String ( count ) ) ;
24+ } ) ;
25+ } ;
26+
27+ const unsubscribe = subscribeToPublishedEvent (
28+ 'events' ,
29+ 'batch' ,
30+ ( { projectId } ) => {
31+ if ( projectId === params . projectId ) {
32+ sendCount ( ) ;
33+ }
2934 }
30- } ) ;
35+ ) ;
36+
37+ const punsubscribe = psubscribeToPublishedEvent (
38+ '__keyevent@0__:expired' ,
39+ ( key ) => {
40+ const [ , , projectId ] = key . split ( ':' ) ;
41+ if ( projectId === params . projectId ) {
42+ sendCount ( ) ;
43+ }
44+ }
45+ ) ;
3146
3247 socket . on ( 'close' , ( ) => {
3348 unsubscribe ( ) ;
49+ punsubscribe ( ) ;
3450 } ) ;
3551}
3652
@@ -42,18 +58,10 @@ export async function wsProjectEvents(
4258 } ;
4359 Querystring : {
4460 token ?: string ;
45- type ?: 'saved' | 'received' ;
4661 } ;
47- } > ,
62+ } >
4863) {
49- const { params, query } = req ;
50- const type = query . type || 'saved' ;
51-
52- if ( ! [ 'saved' , 'received' ] . includes ( type ) ) {
53- socket . send ( 'Invalid type' ) ;
54- socket . close ( ) ;
55- return ;
56- }
64+ const { params } = req ;
5765
5866 const userId = req . session ?. userId ;
5967 if ( ! userId ) {
@@ -67,24 +75,20 @@ export async function wsProjectEvents(
6775 projectId : params . projectId ,
6876 } ) ;
6977
78+ if ( ! access ) {
79+ socket . send ( 'No access' ) ;
80+ socket . close ( ) ;
81+ return ;
82+ }
83+
7084 const unsubscribe = subscribeToPublishedEvent (
7185 'events' ,
72- type ,
73- async ( event ) => {
74- if ( event . projectId === params . projectId ) {
75- const profile = await getProfileById ( event . profileId , event . projectId ) ;
76- socket . send (
77- superjson . stringify (
78- access
79- ? {
80- ...event ,
81- profile,
82- }
83- : transformMinimalEvent ( event ) ,
84- ) ,
85- ) ;
86+ 'batch' ,
87+ ( { projectId, count } ) => {
88+ if ( projectId === params . projectId ) {
89+ socket . send ( setSuperJson ( { count } ) ) ;
8690 }
87- } ,
91+ }
8892 ) ;
8993
9094 socket . on ( 'close' , ( ) => unsubscribe ( ) ) ;
@@ -96,7 +100,7 @@ export async function wsProjectNotifications(
96100 Params : {
97101 projectId : string ;
98102 } ;
99- } > ,
103+ } >
100104) {
101105 const { params } = req ;
102106 const userId = req . session ?. userId ;
@@ -123,9 +127,9 @@ export async function wsProjectNotifications(
123127 'created' ,
124128 ( notification ) => {
125129 if ( notification . projectId === params . projectId ) {
126- socket . send ( superjson . stringify ( notification ) ) ;
130+ socket . send ( setSuperJson ( notification ) ) ;
127131 }
128- } ,
132+ }
129133 ) ;
130134
131135 socket . on ( 'close' , ( ) => unsubscribe ( ) ) ;
@@ -137,7 +141,7 @@ export async function wsOrganizationEvents(
137141 Params : {
138142 organizationId : string ;
139143 } ;
140- } > ,
144+ } >
141145) {
142146 const { params } = req ;
143147 const userId = req . session ?. userId ;
@@ -164,7 +168,7 @@ export async function wsOrganizationEvents(
164168 'subscription_updated' ,
165169 ( message ) => {
166170 socket . send ( setSuperJson ( message ) ) ;
167- } ,
171+ }
168172 ) ;
169173
170174 socket . on ( 'close' , ( ) => unsubscribe ( ) ) ;
0 commit comments