#websocket
9 messages · Page 1 of 1 (latest)
If you're using the Node adapter, you can build Astro in middleware mode. Then you can add both the built Astro project, and socket.io to a server, like express.
https://docs.astro.build/en/guides/integrations-guide/node/#middleware
Okey tysm
hi guys!!!
do you know how to avoid lose the connection on page change?
i don't want the user reconnects every time access a new page...
I tried with transition:persist without success.
components/Websocket.astro
<script>
import { io } from "socket.io-client";
import { getCookies } from "../helpers/utils";
const cookies: Record<string, string> = getCookies();
const websocket = io("http://localhost:3000", {
auth: {
token: cookies["token"] ?? null,
},
});
websocket.on("connect", () => {
console.log("Connected", websocket.id);
});
websocket.on("disconnect", () => {
console.log("Disconnected");
});
if (!window.websocket) {
window.websocket = websocket;
}
</script>
Layouts/Layout.astro
---
import { ViewTransitions } from "astro:transitions";
import Websocket from "../components/Websocket.astro";
---
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="description" content="Astro description" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="generator" content={Astro.generator} />
<ViewTransitions />
</head>
<body>
<slot />
<Websocket transition:persist />
</body>
</html>
@proper sundial hello Xino, have u found a workaround for this ?