im developing a test application (for now) thats using Nextjs/redis.
I have the sse endpoint working (sort-of) correct. When a client connects it retrieves a Snapshot and sends it back, when an update was pushed (with mock route), it should send an Update event with payload (it sends it to the second user onwards)
mock/route.ts
import { NextRequest } from 'next/server';
import Service from '@/models/Service';
import { randomUUID } from 'node:crypto';
import redis from '@/lib/redis';
import { sendUpdateToClients } from '@/lib/sse';
import EventType from '@/models/EventType';
const topic = 'services';
export async function POST(req: NextRequest) {
console.log('<- [POST] on /api/services/mock');
console.log('Adding a mock service');
const u = randomUUID().toString();
const service: Service = {
id: u,
name: `${u}.service`,
active: true
};
try {
const cachedData = await redis.get(topic);
let current: Service[] = [];
if (!cachedData) {
console.log('No cached data, initializing empty array.');
} else {
current = JSON.parse(cachedData);
console.log('Parsed cached data:', current);
}
current.push(service);
await redis.set(topic, JSON.stringify(current));
console.log('Data saved successfully.');
sendUpdateToClients(topic, {
eventType: EventType.UPDATE,
payload: service
});
console.log('Clients updated with new service:', service);
} catch (error) {
console.error('Error handling POST /api/services/mock:', error);
}
return new Response(null, {
status: 204
});
}
sse/route
import type { NextRequest } from 'next/server';
import { addClient, removeClient } from '@/lib/sse';
import redis from '@/lib/redis';
import EventType from '@/models/EventType';
import Event from '@/models/Event';
import Service from '@/models/Service';
export const dynamic = 'force-dynamic';
const topic = 'services';
export async function GET(req: NextRequest) {
console.log('<- [GET] on /api/services/sse');
const encoder = new TextEncoder();
const { readable, writable } = new TransformStream();
const writer = writable.getWriter();
function handleAbort() {
removeClient(topic, writer);
writer.close().then(() => console.log('writer closed'));
}
addClient(topic, writer);
const cachedData = await redis.get(topic);
let services: Service[];
if (cachedData) {
console.log('Cached data found');
services = JSON.parse(cachedData);
} else {
console.log('Cached data not found');
services = [];
}
const payload: Event<Service[]> = {
eventType: EventType.SNAPSHOT,
payload: services
};
writer
.write(encoder.encode(`data: ${JSON.stringify(payload)}nn`))
.then(() => console.log('Snapshot sent to client'))
.catch(err => console.error('Error sending snapshot to client:', err));
req.signal.addEventListener('abort', handleAbort);
return new Response(readable, {
headers: {
'Content-Type': 'text/event-stream; charset=utf-8',
Connection: 'keep-alive',
'Cache-Control': 'no-cache, no-transform',
'Content-Encoding': 'none'
}
});
}
lib/sse
import type Event from '@/models/Event';
interface ClientsMap {
[key: string]: WritableStreamDefaultWriter<any>[];
}
const clients: ClientsMap = {};
export const addClient = (
topic: string,
writer: WritableStreamDefaultWriter<any>
) => {
if (!clients[topic]) {
clients[topic] = [];
}
clients[topic].push(writer);
console.log(
`Client added to topic ${topic}. Total clients for ${topic}: ${clients[topic].length}`
);
console.log(JSON.stringify(clients));
};
export const removeClient = (
topic: string,
writer: WritableStreamDefaultWriter<any>
) => {
if (clients[topic]) {
const index = clients[topic].indexOf(writer);
if (index !== -1) {
clients[topic].splice(index, 1);
}
if (clients[topic].length === 0) {
delete clients[topic];
}
}
console.log(
`Client removed from topic ${topic}. Remaining clients for ${topic}: ${clients[topic] ? clients[topic].length : 0}`
);
};
export const sendUpdateToClients = (topic: string, payload: Event<any>) => {
console.log(JSON.stringify(clients));
// todo: why does it think theres no clients on first connect
// or when theres no data? meaning should return []
// even tho there is at least one? (connected on sse)
//is it somehow closing connection or something? but it's not closing the status (postman req is still running)
// run:
// 1. open 1 sse , open 2nd one
// 2. send mock post -> nothing received on either
// 3. close 2nd sse
// 4. send mock post -> received update on 2nd, but not on first
// 5. close 1st sse
// 6. send mock post -> received on both
if (!clients[topic]) {
console.log(`No clients for topic ${topic}`);
return;
}
console.log(
`Sending update to ${clients[topic].length} clients on topic ${topic}`
);
clients[topic].forEach(client => {
client
.write(
new TextEncoder().encode(`data: ${JSON.stringify(payload)}nn`)
)
.then(() => console.log('Update sent to client'))
.catch(err =>
console.error('Error sending update to client:', err)
);
});
};
It does so, but only when theres at least one client present.
In next server logs i can see that
✓ Ready in 1120ms
docker-svm-web-1 | ✓ Compiled /api/services/sse in 172ms (96 modules)
docker-svm-web-1 | <- [GET] on /api/services/sse
docker-svm-web-1 | Client added to topic services. Total clients for services: 1
docker-svm-web-1 | {"services":[{}]}
docker-svm-redis-1 | 1:M 19 May 2024 14:05:06.244 * DB saved on disk
docker-svm-web-1 | Redis data flushed: OK
docker-svm-web-1 | Cached data not found
docker-svm-web-1 | Snapshot sent to client
docker-svm-web-1 | ✓ Compiled /api/services/mock in 69ms (99 modules)
docker-svm-web-1 | <- [POST] on /api/services/mock
docker-svm-web-1 | Adding a mock service
docker-svm-redis-1 | 1:M 19 May 2024 14:05:19.834 * DB saved on disk
docker-svm-web-1 | Redis data flushed: OK
docker-svm-web-1 | No cached data, initializing empty array.
docker-svm-web-1 | Data saved successfully.
docker-svm-web-1 | {}
docker-svm-web-1 | No clients for topic services
docker-svm-web-1 | Clients updated with new service: {
docker-svm-web-1 | id: '7f90d7db-0a5b-4d2e-ab72-ffb18587c125',
docker-svm-web-1 | name: '7f90d7db-0a5b-4d2e-ab72-ffb18587c125.service',
docker-svm-web-1 | active: true
docker-svm-web-1 | }
docker-svm-web-1 | POST /api/services/mock 204 in 113ms
there is no clients, even though the client was added.
No clients were closed / postman / curl running as expected
The following line after “Data saved successfully” is a client dump
full repo: https://github.com/kamillcodes/docker-svm
any ideas why it would be happening? definitely something simple that im not seeing
Tried adding plentiful of logs, hardcoding some dummy clients trying to figure out why first client is not populated correctly.
Kamil L is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.