Skip to content

Commit c7331ea

Browse files
Merge pull request #7 from ably-labs/refactor/implement-ts-and-general-overhaul
Refactor: Refactor to use ts and netlify functions
2 parents 86872a6 + f4e4a64 commit c7331ea

26 files changed

+22748
-2615
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ABLY_API_KEY=

.eslintrc.json

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
11
{
2-
"extends": "next/core-web-vitals"
2+
"extends": [
3+
"next/core-web-vitals",
4+
"prettier",
5+
"plugin:react/recommended"
6+
],
7+
"plugins": [
8+
"react"
9+
],
10+
"rules": {
11+
"react/react-in-jsx-scope": "off",
12+
"react/prop-types": "off"
13+
},
14+
"settings": {
15+
"react": {
16+
"version": "detect"
17+
}
18+
},
19+
"parserOptions": {
20+
"ecmaFeatures": {
21+
"jsx": true
22+
},
23+
"ecmaVersion": 2021,
24+
"sourceType": "module"
25+
}
326
}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ yarn-error.log*
2929
.env.development.local
3030
.env.test.local
3131
.env.production.local
32+
33+
# local Netlify folder
34+
.netlify

.tool-versions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nodejs 20.10.0

README.md

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Building a Realtime Chat App with Next.js, Ably, and Vercel
1+
# Building a Realtime Chat App with Next.js, Ably, and Netlify
22

33
Live example at: <https://next-js-chat-app.vercel.app>
44

@@ -18,20 +18,22 @@ It demonstrates the use of:
1818

1919
The project uses the following components:
2020

21-
- [Next.js](https://nextjs.org/) is a React framework from [Vercel](https://vercel.com/). It is used to build static web applications with server side rendering, serverless functions and seamless hosting. It's a framework that takes the React knowledge you already have, and puts some structure and conventions in place.
21+
- [Next.js](https://nextjs.org/) is a React framework used to build static web applications with server side rendering, serverless functions and seamless hosting. It's a framework that takes the React knowledge you already have, and puts some structure and conventions in place.
2222

2323
- [Ably](https://ably.com/) is realtime, pub/sub messaging platform with a suite of integrated services to deliver complete realtime functionality directly to end-users.
2424

25-
- [Vercel](https://vercel.com/) is a hosting platform, built from the ground up to host Next.js apps, and Serverless Functions with them.
25+
- [Netlify](https://netlify.com/) is a hosting platform that provides continuous deployment, serverless functions, and a global CDN for modern web projects.
2626

2727
- [React](https://reactjs.org/) is a JavaScript library for building user interfaces with encapsulated components that manage their own state.
2828

29+
- [TypeScript](https://www.typescriptlang.org/) is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.
30+
2931
## What are we going to build?
3032

31-
![The UI of the chat app we'll build. It is a window with speech bubbles for text.](https://cdn.glitch.com/0cb30add-c9ef-4c00-983c-e12deb0d4080%2Fchatapp.png?v=1612279601157)
32-
*The UI of the app we'll build with this walkthrough*
33+
![The UI of the chat app we'll build. It is a window with speech bubbles for text.](https://cdn.glitch.com/0cb30add-c9ef-4c00-983c-e12deb0d4080%2Fchatapp.png?v=1612279601157)
34+
*The UI of the app we'll build with this walkthrough*
3335

34-
We'll build a realtime chat app that runs in the browser. It will be built upon the Next.js [create-next-app](https://nextjs.org/docs/api-reference/create-next-app) template, it will contain a React component which will use Ably to send and receive messages. We'll also write a Next.js serverless function which will be used to connect to Ably.
36+
We'll build a realtime chat app that runs in the browser. It will be built upon the Next.js [create-next-app](https://nextjs.org/docs/api-reference/create-next-app) template, it will contain a React component which will use Ably to send and receive messages. We'll also write a serverless function which will be used to authenticate the client with Ably.
3537

3638
## Building & running locally
3739

@@ -40,8 +42,8 @@ We'll build a realtime chat app that runs in the browser. It will be built upon
4042
In order to build and deploy this app, you will need:
4143

4244
- **An Ably account** for sending messages: [Create an account with Ably for free](https://ably.com/signup).
43-
- **A Vercel Account** for hosting on production: [Create an account with Vercel for free](https://vercel.com/signup).
44-
- **Node 16** or greater: [Install Node](https://nodejs.org/en/).
45+
- **A Netlify Account** for hosting on production: [Create an account with Netlify for free](https://netlify.com/signup).
46+
- **Node 20** or greater: [Install Node](https://nodejs.org/en/).
4547

4648
You'll also need an API key from Ably to authenticate with the Ably Service. To get an API key, once you have [created an Ably account](https://ably.com/signup):
4749

@@ -58,24 +60,35 @@ ABLY_API_KEY=your-ably-api-key:goes-here
5860
```
5961

6062
2. Run `npm install`.
61-
3. Run `npm run dev`.
63+
3. Run `npm run netlify:dev` to start the Netlify development server.
64+
65+
The Netlify development server will spin up and you'll see a demo chat application.
66+
67+
> Note: You can also use `npm run dev` to run just the Next.js development server without Netlify functions, but for the full experience with serverless functions, use `npm run netlify:dev`.
68+
69+
### TypeScript Support
70+
71+
This project uses TypeScript for type safety. Here are some useful commands for TypeScript development:
72+
73+
- `npm run type-check`: Run TypeScript's type checker without emitting files
74+
- `npm run dev`: The Next.js development server automatically type-checks your code as you develop
6275

63-
The Next.js dev server will spin up and you'll see a demo chat application.
76+
When creating new components or functions, use the `.tsx` extension for React components and `.ts` for plain TypeScript files. The project is configured with a `tsconfig.json` file that provides sensible defaults for Next.js TypeScript projects.
6477

65-
## Hosting on Vercel
78+
## Hosting on Netlify
6679

67-
We're using `Vercel` as our development server and build pipeline.
80+
We're using `Netlify` as our development server and build pipeline.
6881

69-
> The easiest way to deploy Next.js to production is to use the Vercel platform from the creators of Next.js. Vercel is an all-in-one platform with Global CDN supporting static & Jamstack deployment and Serverless Functions.
70-
<cite>-- [The Next.js documentation](https://nextjs.org/docs/deployment)</cive>
82+
> Netlify is a powerful platform for building, deploying, and managing modern web projects. It provides continuous deployment, serverless functions, and a global CDN.
83+
<cite>-- [The Netlify documentation](https://docs.netlify.com/)</cite>
7184
72-
In order to deploy your new chat app to Vercel you'll need to:
85+
In order to deploy your new chat app to Netlify you'll need to:
7386

7487
1. Create a [GitHub account](https://github.com/) (if you don't already have one)
7588
2. [Push your app to a GitHub repository](https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-new-repository)
76-
3. [Create a Vercel account](https://vercel.com/signup)
77-
4. Create a new Vercel app and import your app from your GitHub repository. (This will require you to authorise Vercel to use your GitHub account)
78-
5. Add your `ABLY_API_KEY` as an environment variable
89+
3. [Create a Netlify account](https://netlify.com/signup)
90+
4. Create a new Netlify site and import your app from your GitHub repository. (This will require you to authorize Netlify to use your GitHub account)
91+
5. Add your `ABLY_API_KEY` as an environment variable in the Netlify site settings
7992
6. Watch your app deploy
8093
7. Visit the newly created URL in your browser!
8194

app/layout.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

app/layout.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import './globals.css';
2+
import React from 'react';
3+
import { Metadata } from 'next';
4+
5+
export const metadata: Metadata = {
6+
title: 'Next.js',
7+
description: 'Generated by Next.js',
8+
};
9+
10+
export default function RootLayout({ children }: { children: React.ReactNode }) {
11+
return (
12+
<html lang="en">
13+
<body>{children}</body>
14+
</html>
15+
);
16+
}

app/page.js renamed to app/page.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
1-
import Head from 'next/head';
2-
import dynamic from 'next/dynamic';
3-
4-
const Chat = dynamic(() => import('../components/Chat'), {
5-
ssr: false,
6-
});
1+
import Chat from '@/components/Chat';
72

83
export default function Home() {
94
return (
105
<div className="container">
11-
<Head>
12-
<title>Realtime Chat App with Ably Chat, Next.js and Netlify</title>
13-
<link rel="icon" href="https://static.ably.dev/motif-red.svg?nextjs-netlify" type="image/svg+xml" />
14-
</Head>
156
<main>
167
<h1 className="title">Ably Chat Demo</h1>
178
<Chat />

components/Chat.jsx

Lines changed: 0 additions & 23 deletions
This file was deleted.

components/Chat.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use client';
2+
3+
import * as Ably from 'ably';
4+
import { ChatClient } from '@ably/chat';
5+
import { ChatClientProvider, ChatRoomProvider } from '@ably/chat/react';
6+
import ChatBox from './ChatBox';
7+
import React, { useEffect, useState } from 'react';
8+
9+
export default function Chat() {
10+
const [chatClient, setChatClient] = useState<ChatClient | null>(null);
11+
12+
useEffect(() => {
13+
const clientId = `ably-chat-demo-user-${Math.random().toString(36).substring(2, 10)}`;
14+
const realtimeClient = new Ably.Realtime({ authUrl: `/api/auth?clientId=${clientId}`, clientId });
15+
const ablyChatClient = new ChatClient(realtimeClient);
16+
17+
setChatClient(ablyChatClient);
18+
return () => {
19+
realtimeClient.close();
20+
};
21+
}, []);
22+
23+
if (!chatClient) {
24+
return <div>Loading...</div>;
25+
}
26+
27+
return (
28+
<ChatClientProvider client={chatClient}>
29+
<ChatRoomProvider id="chat-demo">
30+
<ChatBox />
31+
</ChatRoomProvider>
32+
</ChatClientProvider>
33+
);
34+
}

0 commit comments

Comments
 (0)