elysiajs / elysia-trpc

A plugin for Elysia that add support for using tRPC
MIT License
18 stars 17 forks source link

Websockets not calling unsubscribe and error at connect #14

Open Jonatthu opened 7 months ago

Jonatthu commented 7 months ago

Getting the screenshot error on connect and also disconnect, also since is erroring out, it's not capable to unsubscribe.

import { appRouter, createTRPCContextFetch } from '@app-packages/api';
import { cors } from '@elysiajs/cors';
import { html } from '@elysiajs/html';
import { trpc } from '@elysiajs/trpc';
import dotenv from 'dotenv';
import { Elysia } from 'elysia';
import { renderTrpcPanel } from 'trpc-panel';

dotenv.config({
    override: true,
    path: '../../.env',
});

const app = new Elysia().use(html()).use(cors());

app.get('/panel', (context) => {
    context.headers['Content-Type'] = 'text/html; charset=utf-8';

    return renderTrpcPanel(appRouter, {
        url: 'http://localhost:8080/api/trpc',
        transformer: 'superjson',
    });
});

app.use(
    trpc(appRouter, {
        endpoint: '/api/trpc',
        createContext: createTRPCContextFetch,
    })
);

app.listen(8080);

// eslint-disable-next-line no-console
console.log('🦊 Server started on http://localhost:8080/panel');

with dependencies:

  "dependencies": {
    "@app-packages/api": "*",
    "@elysiajs/cors": "^0.8.0",
    "@elysiajs/html": "^0.8.0",
    "@elysiajs/trpc": "^0.8.0",
    "@trpc/server": "^10.45.0",
    "elysia": "^0.8.9",
    "trpc-panel": "^1.3.4"
  },
  "devDependencies": {
    "bun-types": "^1.0.22",
    "@types/ws": "^8.5.10",
    "@app/eslint-config": "*"
  },

The code itself:

    onNotifyClubChat: publicProcedure.subscription(async () => {
        console.log('onNotifyClubChat');

        return observable<string>((emit) => {
            redisSubscriber.subscribe('club-chat');

            const onMessage = (channel: string, message: string) => {
                console.log('onNotifyClubChat message', channel, message);
                emit.next(`${channel}:${message}`);
            };
            redisSubscriber.on('message', onMessage);

            return () => {
                console.log('onNotifyClubChat unsubscribed');
                redisSubscriber.off('club-chat', onMessage);
            };
        });
    }),

The subscription is successfully sending the message or subscribing regardless of the error, but it's not unsubscribing, and that causes a memory leak.

image
Jonatthu commented 7 months ago

It seems that data.id is comming undefined in both cases also in close

image image