batching events
This commit is contained in:
committed by
Carl-Gerhard Lindesvärd
parent
244aa3b0d3
commit
5e225b7ae6
@@ -19,7 +19,7 @@ export function getLiveEventInfo(key: string) {
|
||||
return key.split(':').slice(2) as [string, string];
|
||||
}
|
||||
|
||||
export async function test(
|
||||
export async function testVisitors(
|
||||
req: FastifyRequest<{
|
||||
Params: {
|
||||
projectId: string;
|
||||
@@ -28,13 +28,15 @@ export async function test(
|
||||
reply: FastifyReply
|
||||
) {
|
||||
const events = await getEvents(
|
||||
`SELECT * FROM events WHERE project_id = ${escape(req.params.projectId)} AND name = 'screen_view' LIMIT 500`
|
||||
`SELECT * FROM events LIMIT 500`
|
||||
// `SELECT * FROM events WHERE name = 'screen_view' LIMIT 500`
|
||||
);
|
||||
const event = events[Math.floor(Math.random() * events.length)];
|
||||
if (!event) {
|
||||
return reply.status(404).send('No event found');
|
||||
}
|
||||
redisPub.publish('event', superjson.stringify(event));
|
||||
event.projectId = req.params.projectId;
|
||||
redisPub.publish('event:received', superjson.stringify(event));
|
||||
redis.set(
|
||||
`live:event:${event.projectId}:${Math.random() * 1000}`,
|
||||
'',
|
||||
@@ -44,6 +46,26 @@ export async function test(
|
||||
reply.status(202).send(event);
|
||||
}
|
||||
|
||||
export async function testEvents(
|
||||
req: FastifyRequest<{
|
||||
Params: {
|
||||
projectId: string;
|
||||
};
|
||||
}>,
|
||||
reply: FastifyReply
|
||||
) {
|
||||
const events = await getEvents(
|
||||
`SELECT * FROM events LIMIT 500`
|
||||
// `SELECT * FROM events WHERE name = 'screen_view' LIMIT 500`
|
||||
);
|
||||
const event = events[Math.floor(Math.random() * events.length)];
|
||||
if (!event) {
|
||||
return reply.status(404).send('No event found');
|
||||
}
|
||||
redisPub.publish('event:saved', superjson.stringify(event));
|
||||
reply.status(202).send(event);
|
||||
}
|
||||
|
||||
export function wsVisitors(
|
||||
connection: {
|
||||
socket: WebSocket;
|
||||
@@ -56,11 +78,11 @@ export function wsVisitors(
|
||||
) {
|
||||
const { params } = req;
|
||||
|
||||
redisSub.subscribe('event');
|
||||
redisSub.subscribe('event:received');
|
||||
redisSub.psubscribe('__key*:expired');
|
||||
|
||||
const message = (channel: string, message: string) => {
|
||||
if (channel === 'event') {
|
||||
if (channel === 'event:received') {
|
||||
const event = getSuperJson<IServiceCreateEventPayload>(message);
|
||||
if (event?.projectId === params.projectId) {
|
||||
getLiveVisitors(params.projectId).then((count) => {
|
||||
@@ -82,7 +104,7 @@ export function wsVisitors(
|
||||
redisSub.on('pmessage', pmessage);
|
||||
|
||||
connection.socket.on('close', () => {
|
||||
redisSub.unsubscribe('event');
|
||||
redisSub.unsubscribe('event:saved');
|
||||
redisSub.punsubscribe('__key*:expired');
|
||||
redisSub.off('message', message);
|
||||
redisSub.off('pmessage', pmessage);
|
||||
@@ -90,7 +112,7 @@ export function wsVisitors(
|
||||
}
|
||||
|
||||
export function wsEvents(connection: { socket: WebSocket }) {
|
||||
redisSub.subscribe('event');
|
||||
redisSub.subscribe('event:saved');
|
||||
|
||||
const message = (channel: string, message: string) => {
|
||||
const event = getSuperJson<IServiceCreateEventPayload>(message);
|
||||
@@ -102,7 +124,7 @@ export function wsEvents(connection: { socket: WebSocket }) {
|
||||
redisSub.on('message', message);
|
||||
|
||||
connection.socket.on('close', () => {
|
||||
redisSub.unsubscribe('event');
|
||||
redisSub.unsubscribe('event:saved');
|
||||
redisSub.off('message', message);
|
||||
});
|
||||
}
|
||||
@@ -129,7 +151,7 @@ export async function wsProjectEvents(
|
||||
projectId: params.projectId,
|
||||
});
|
||||
|
||||
redisSub.subscribe('event');
|
||||
redisSub.subscribe('event:saved');
|
||||
|
||||
const message = async (channel: string, message: string) => {
|
||||
const event = getSuperJson<IServiceCreateEventPayload>(message);
|
||||
@@ -151,7 +173,7 @@ export async function wsProjectEvents(
|
||||
redisSub.on('message', message as any);
|
||||
|
||||
connection.socket.on('close', () => {
|
||||
redisSub.unsubscribe('event');
|
||||
redisSub.unsubscribe('event:saved');
|
||||
redisSub.off('message', message as any);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user