SSE Update issues for the first client

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.

New contributor

Kamil L is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật