Skip to content

Commit

Permalink
docs: add example with Next.js (with app router)
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Mar 27, 2024
1 parent b0568b2 commit 61f4449
Show file tree
Hide file tree
Showing 36 changed files with 74 additions and 127 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Expand Up @@ -55,8 +55,8 @@ jobs:
- webpack-build-server
- basic-crud-application/angular-client
- basic-crud-application/vue-client
- nextjs-pages-router-api-route
- nextjs-pages-router-custom-server
- nextjs-pages-router
- nextjs-app-router

steps:
- name: Checkout repository
Expand Down
File renamed without changes.
Expand Up @@ -16,11 +16,7 @@ bun dev

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file.

[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.js`.

The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.
You can start editing the page by modifying `app/page.js`. The page auto-updates as you edit the file.

This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.

Expand Down
File renamed without changes.
@@ -1,6 +1,4 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
};
const nextConfig = {};

export default nextConfig;
@@ -1,7 +1,8 @@
{
"name": "nextjs-pages-router-api-route",
"name": "nextjs-app-router",
"version": "0.1.0",
"private": true,
"type": "module",
"scripts": {
"dev": "next dev",
"build": "next build",
Expand Down
File renamed without changes
@@ -1,5 +1,4 @@
import { createServer } from "http";
import { parse } from "url";
import next from "next";
import { Server } from "socket.io";

Expand All @@ -8,25 +7,12 @@ const hostname = "localhost";
const port = 3000;
// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();
const handler = app.getRequestHandler();

app.prepare().then(() => {
const httpServer = createServer(async (req, res) => {
try {
const parsedUrl = parse(req.url, true);
const { pathname, query } = parsedUrl;
const httpServer = createServer(handler);

await handle(req, res, parsedUrl);
} catch (err) {
console.error("Error occurred handling", req.url, err);
res.statusCode = 500;
res.end("internal server error");
}
});

const io = new Server(httpServer, {
pingInterval: 2000
});
const io = new Server(httpServer);

io.on("connection", (socket) => {
// ...
Expand Down
17 changes: 17 additions & 0 deletions examples/nextjs-app-router/src/app/layout.js
@@ -0,0 +1,17 @@
import { Inter } from "next/font/google";
import "./globals.css";

const inter = Inter({ subsets: ["latin"] });

export const metadata = {
title: "Create Next App",
description: "Generated by create next app",
};

export default function RootLayout({ children }) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
</html>
);
}
@@ -1,11 +1,9 @@
import Head from "next/head";
"use client";

import Image from "next/image";
import { Inter } from "next/font/google";
import styles from "@/styles/Home.module.css";
import styles from "./page.module.css";
import { useEffect, useState } from "react";
import { socket } from "@/socket";

const inter = Inter({ subsets: ["latin"] });
import { socket } from "../socket";

export default function Home() {
const [isConnected, setIsConnected] = useState(false);
Expand Down Expand Up @@ -40,17 +38,9 @@ export default function Home() {
}, []);

return (
<>
<Head>
<title>Create Next App</title>
<meta name="description" content="Generated by create next app" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="/favicon.ico" />
</Head>
<div>
<p>Status: { isConnected ? "connected" : "disconnected" }</p>
<p>Transport: { transport }</p>
</div>
</>
<div>
<p>Status: { isConnected ? "connected" : "disconnected" }</p>
<p>Transport: { transport }</p>
</div>
);
}
Expand Up @@ -42,8 +42,8 @@
.grid {
display: grid;
grid-template-columns: repeat(4, minmax(25%, auto));
max-width: var(--max-width);
width: 100%;
max-width: 100%;
width: var(--max-width);
}

.card {
Expand All @@ -70,6 +70,7 @@
font-size: 0.9rem;
line-height: 1.5;
max-width: 30ch;
text-wrap: balance;
}

.center {
Expand Down
5 changes: 5 additions & 0 deletions examples/nextjs-app-router/src/socket.js
@@ -0,0 +1,5 @@
"use client";

import { io } from "socket.io-client";

export const socket = io();
5 changes: 0 additions & 5 deletions examples/nextjs-pages-router-api-route/src/pages/_app.js

This file was deleted.

45 changes: 0 additions & 45 deletions examples/nextjs-pages-router-api-route/src/pages/api/socket.io.js

This file was deleted.

9 changes: 0 additions & 9 deletions examples/nextjs-pages-router-api-route/src/socket.js

This file was deleted.

13 changes: 0 additions & 13 deletions examples/nextjs-pages-router-custom-server/src/pages/_document.js

This file was deleted.

This file was deleted.

Expand Up @@ -12,6 +12,7 @@
# next.js
/.next/
/out/
/src/.next/

# production
/build
Expand Down
File renamed without changes.
File renamed without changes.
@@ -1,5 +1,5 @@
{
"name": "nextjs-pages-router-custom-server",
"name": "nextjs-pages-router",
"version": "0.1.0",
"private": true,
"type": "module",
Expand Down
29 changes: 29 additions & 0 deletions examples/nextjs-pages-router/server.js
@@ -0,0 +1,29 @@
import { createServer } from "http";
import next from "next";
import { Server } from "socket.io";

const dev = process.env.NODE_ENV !== "production";
const hostname = "localhost";
const port = 3000;
// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port });
const handler = app.getRequestHandler();

app.prepare().then(() => {
const httpServer = createServer(handler);

const io = new Server(httpServer);

io.on("connection", (socket) => {
// ...
});

httpServer
.once("error", (err) => {
console.error(err);
process.exit(1);
})
.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`);
});
});
Expand Up @@ -18,7 +18,7 @@ export default function Home() {

function onConnect() {
setIsConnected(true);
setTransport(socket.io.engine.transport.name || "N/A");
setTransport(socket.io.engine.transport.name);

socket.io.engine.on("upgrade", (transport) => {
setTransport(transport.name);
Expand Down
File renamed without changes.

0 comments on commit 61f4449

Please sign in to comment.