io.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import http from "http";
  2. import bcrypt from "bcryptjs";
  3. import { Server } from "socket.io";
  4. import { instrument, RedisStore } from "@socket.io/admin-ui";
  5. import { createAdapter } from "@socket.io/redis-adapter";
  6. import { pubClient, subClient } from "../connection/redis.js";
  7. import { logger } from "./logger.js";
  8. import customParser from "socket.io-msgpack-parser";
  9. console.log("process.env.SOCKET_PATH", process.env.SOCKET_PATH);
  10. const httpServer = http.createServer();
  11. const io = new Server(httpServer, {
  12. cors: {
  13. origin: "*",
  14. },
  15. path: process.env.SOCKET_PATH || "",
  16. pingInterval: 10000,
  17. pingTimeout: 5000,
  18. parser: customParser,
  19. });
  20. instrument(io, {
  21. auth: {
  22. type: "basic",
  23. username: process.env.WATCH_USER,
  24. password: bcrypt.hashSync(process.env.WATCH_PASSWORD, 10),
  25. },
  26. namespaceName: "/watch",
  27. });
  28. Promise.all([pubClient.connect(), subClient.connect()])
  29. .then(() => {
  30. io.adapter(createAdapter(pubClient, subClient));
  31. logger.info("redis is conetcted");
  32. })
  33. .catch((error) => {
  34. logger.error("redis is connect fail", error);
  35. });
  36. export { io };