Hello, I already have a express + express-ws server set up and I’d like to implement websockets for graphql subscriptions. The guide says that I need to create a websocket server using the ws package. Won’t that interfere with the express-ws server? This is my current main file:
/* --------------------------- External Libraries --------------------------- */
import express from "express";
import bodyParser, { json } from "body-parser";
import { ApolloServer } from "@apollo/server";
import { expressMiddleware } from "@apollo/server/express4";
import compression from "compression";
import cors from "cors";
import "reflect-metadata";
import expressWs from "express-ws";
/* ---------------------------- Internal Imports ---------------------------- */
import { DB } from "database/data-source";
/* ----------------------------- Setup Variables ---------------------------- */
const app = expressWs(express()).app;
app.use(
cors({
origin: "*",
}),
bodyParser.json(),
compression()
);
const initiate = async () => {
// TypeORM
await DB.initialize();
// GraphQL
const gqlServer = new ApolloServer({
typeDefs: require("./graphql/schema").default,
resolvers: require("./graphql/resolvers").default,
});
await gqlServer.start();
// Endpoints
app.use("/v1/", require("./routes/v1/router").default);
app.use("/graphql", expressMiddleware(gqlServer));
// Server
app.listen(3000, () => {
console.log("\x1b[34mExpress \x1b[37m » API ready and listening on port 3000.");
});
};
initiate();