Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rnbrady committed Oct 6, 2024
1 parent 5e9b8fb commit 91d4504
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 98 deletions.
37 changes: 37 additions & 0 deletions app/api/basic/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// This serverless function connects to a remote websocket echo server, sends
// messages to it and logs any responses to the console.

// Remove this line and the bug disappears
export const runtime = "edge";

export async function GET(req) {
const ws = new WebSocket("ws://localhost:3030");

ws.addEventListener("error", console.error);

let timer;

ws.addEventListener("open", function open() {
let counter = 0;

timer = setInterval(() => {
const message = counter.toString();

ws.send(message);

console.log("sent: ", message);

counter++;
}, 1000);
});

ws.addEventListener("message", function message(event) {
console.log("received: %s", event.data);
});

await new Promise((resolve) => setTimeout(resolve, 60000));

clearInterval(timer);

return new Response("Test complete after running for 60 seconds");
}
41 changes: 41 additions & 0 deletions app/api/fulcrum/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Fulcrum is a Bitcoin indexer written in C++ (https://github.com/cculianu/Fulcrum)
// It sends a ping every 20 seconds, so you will need to wait 20 seconds to observe the bug

// Remove this line and the bug disappears
export const runtime = "edge";

export async function GET(req) {
const ws = new WebSocket("ws://bch.imaginary.cash:50003");

ws.addEventListener("error", console.error);

let timer;

ws.addEventListener("open", function open() {
let counter = 0;

setInterval(() => {
const message = counter < 1
? `{"id":0,"method":"server.version","params":["vercel/edge-runtime#983","1.5"]}`
: `{"id":${counter.toString()},"method":"server.ping","params":[]}`;

ws.send(message);

console.log("sent: ", message);

counter++;
}, 1000);


});

ws.addEventListener("message", function message(event) {
console.log("received: %s", event.data);
});

await new Promise((resolve) => setTimeout(resolve, 60000));

clearInterval(timer);

return new Response("Test complete after running for 60 seconds");
}
51 changes: 51 additions & 0 deletions app/api/stream/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// This serverless function connects to an upstream websocket server and
// streams messages to the client.

// Remove this line and the bug disappears
export const runtime = "edge";

export async function GET(req) {
const ws = new WebSocket("ws://localhost:3030");

ws.addEventListener("error", console.error);

let timer;

ws.addEventListener("open", function open() {
let counter = 0;

timer = setInterval(() => {
const message = counter.toString();

ws.send(message);

console.log("sent: ", message);

counter++;
}, 1000);
});


const encoder = new TextEncoder();
const customReadable = new ReadableStream({
start(controller) {
ws.addEventListener("message", function message(event) {
console.log("received: %s", event.data);

controller.enqueue(`event: data\ndata: ${event.data}\n\n`);
});
},
cancel() {
ws.close();

clearInterval(timer);
}
});

return new Response(customReadable, {
headers: {
"Content-Type": "text/event-stream"
}
});

}
94 changes: 3 additions & 91 deletions app/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,98 +3,10 @@ import Image from "next/image";
export default function Home() {
return (
<div className="font-sans grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-16 sm:p-20">
<main className="flex flex-col gap-8 row-start-2 items-center sm:items-start">
<Image
className="dark:invert"
src="/next.svg"
alt="Next.js logo"
width={180}
height={38}
priority
/>
<ol className="font-mono list-inside list-decimal text-sm text-center sm:text-left">
<li className="mb-2">
Get started by editing{" "}
<code className="bg-black/[.05] dark:bg-white/[.06] px-1 py-0.5 rounded font-semibold">
app/page.js
</code>
</li>
<li>Save and see your changes instantly.</li>
</ol>

<div className="flex gap-4 items-center flex-col sm:flex-row">
<a
className="rounded-full border border-solid border-transparent transition-colors flex items-center justify-center bg-foreground text-background gap-2 hover:bg-[#383838] dark:hover:bg-[#ccc] text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5"
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
className="dark:invert"
src="/vercel.svg"
alt="Vercel logomark"
width={20}
height={20}
/>
Deploy now
</a>
<a
className="rounded-full border border-solid border-black/[.08] dark:border-white/[.145] transition-colors flex items-center justify-center hover:bg-[#f2f2f2] dark:hover:bg-[#1a1a1a] hover:border-transparent text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5 sm:min-w-44"
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Read our docs
</a>
</div>
<main className="">
<p>This repo reproduces <a className="underline" href="https://github.com/vercel/edge-runtime/issues/983">vercel/edge-runtime#983</a>.</p>
<p>Run <span className="bg-gray-700 font-mono">node server.mjs</span> in separate terminal and then visit <a className="underline" href="/api/stream">/api/stream</a>.</p>
</main>
<footer className="row-start-3 flex gap-6 flex-wrap items-center justify-center">
<a
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
aria-hidden
src="/file-text.svg"
alt="File icon"
width={16}
height={16}
/>
Learn
</a>
<a
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
aria-hidden
src="/window.svg"
alt="Window icon"
width={16}
height={16}
/>
Examples
</a>
<a
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
href="https://nextjs.org?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
aria-hidden
src="/globe.svg"
alt="Globe icon"
width={16}
height={16}
/>
Go to nextjs.org →
</a>
</footer>
</div>
);
}
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
"lint": "next lint"
},
"dependencies": {
"next": "15.0.0-rc.0",
"react": "19.0.0-rc-f994737d14-20240522",
"react-dom": "19.0.0-rc-f994737d14-20240522",
"next": "15.0.0-rc.0"
"ws": "^8.18.0"
},
"devDependencies": {
"postcss": "^8",
"tailwindcss": "^3.4.1",
"eslint": "^8",
"eslint-config-next": "15.0.0-rc.0"
"eslint-config-next": "15.0.0-rc.0",
"postcss": "^8",
"tailwindcss": "^3.4.1"
}
}
23 changes: 20 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions server.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { WebSocketServer } from 'ws';

const wss = new WebSocketServer({
port: 3030,

});

wss.on('connection', function connection(ws) {
ws.on('message', function message(data) {
console.log('received: %s', data);
ws.send(data.toString());
console.log('sent: %s', data);
});

setInterval(() => ws.ping(), 3000);

ws.on('error', console.error);

console.log('connection opened');
});

wss.on('close', function close() {
console.log('connection closed');
});

0 comments on commit 91d4504

Please sign in to comment.