Replies: 3 comments 8 replies
-
found this: #2622 and this: https://sergiodxa.com/articles/use-server-sent-events-with-remix |
Beta Was this translation helpful? Give feedback.
-
Nobody seems to be talking about this: how can we share objects between a custom server and Remix server side? I have a working solution (before Vite) so let me share about it a bit. ProblemSocket.io requires an instance of HTTP server, so it needs to be placed in server.js // server.js
const express = require("express");
const { createServer } = require("http");
const { Server } = require("socket.io");
const defineSockets = require("./app/server/defineSockets");
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer);
defineSockets(io) Let's have a module to hold sockets so we can use it from Remix // app/server/defineSockets.js
let chat;
export default defineSockets = (io) => {
chat = io.of("chat")
}
export const broadcastComment = (roomId, comment) => {
chat.to(roomId).emit(comment)
} However, this // app/routes/chat.tsx
import { broadcastComment } from "~/server/defineSockets";
export const action = () => {
// process request
broadcastComment(comment); /* Error, `chat` is undefined! */
} This is because
How we can make it workMake
Overview of my solutionI'm not very satisfied with how the code looks, so let me just share an overview
export const requireCommon = (moduleName: string) {
return require(moduleName)
}
Future workI'm trying to make it work with Vite but probably switching to Server-Side Events would be much easier |
Beta Was this translation helpful? Give feedback.
-
Do someone managed to run last version remix with socket.io ? |
Beta Was this translation helpful? Give feedback.
-
Just some thought:
I see this example: https://github.com/remix-run/examples/tree/main/socket.io
With this example, I can see the traffic via web-socket has to be client side rendering (via javascript). Is there a better way to move this part rendering to the server side rendering similar as loader via http?
Beta Was this translation helpful? Give feedback.
All reactions