Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
2 replies
reallist

tRPC Websockets | package subpath not defined by exports

So basically I am playing around with tRPC Websockets/Subscriptions.

I am primarily using this repo as a reference -> https://github.com/trpc/examples-next-prisma-websockets-starter

I've created a server.ts which has the following contents ->

import { applyWSSHandler } from "@trpc/server/dist/adapters/ws.js";
import next from "next";
import { createContext } from "./context";
import { parse } from "url";
import http from "http";

import ws from "ws";
import { env } from "../../env/server.mjs";
import { appRouter } from "./router/_app.js";

const port = "3001";
const dev = env.NODE_ENV;
const app = next({});
const handle = app.getRequestHandler();

app.prepare().then(() => {
  const server = http.createServer((req, res) => {
    const proto = req.headers["x-forwarded-proto"];
    if (proto && proto === "http") {
      // redirect to ssl
      res.writeHead(303, {
        location: `https://` + req.headers.host + (req.headers.url ?? ""),
      });
      res.end();
      return;
    }
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    const parsedUrl = parse(req.url!, true);
    handle(req, res, parsedUrl);
  });
  const wss = new ws.Server({ server });
  const handler = applyWSSHandler({ wss, router: appRouter, createContext });

  process.on("SIGTERM", () => {
    console.log("SIGTERM");
    handler.broadcastReconnectNotification();
  });
  server.listen(port);

  // tslint:disable-next-line:no-console
  console.log(
    `> Server listening at http://localhost:${port} as ${
      dev ? "development" : process.env.NODE_ENV
    }`
  );
});


I'm also using the dev version (wssDevServer) for my localhost, which runs without any issues. When I attempt to run this on my custom VPS (centos +
nginx), with
cross-env NODE_ENV=production node dist/server/trpc/server.js
I get the error shown in the screenshot.

Been googling for the past 1 hour, but nothing which really helps me to understand how to approach this issue?
image.png
Was this page helpful?