August 5, 2023
O. Wolfson
Today, I'd like to dive deep into building a real-time chat component using React and Supabase, a robust open-source alternative to Firebase.
View the live demo of the chat component.
Imports & Preliminaries:
We start by importing necessary dependencies. We've also defined a ChatMessage type for better type checking and a delete alert message for user confirmation.
State Management:
messages: To store chat messages.currentMessage: Keeps track of the current message being typed.avatars: A mapping of user IDs to their respective avatars.chatId: Static ID representing the chat room.hoveredMessageId: Keeps track of which message is being hovered over for contextual UI actions.Fetching and Displaying Messages:
getData function retrieves messages for the current chat room.setAvatarsBySenderIds gathers avatars for the users.Sending Messages:
handleSend function allows users to send messages, which are stored in the Supabase database.Real-Time Functionality:
We're using the useEffect hook to subscribe to real-time changes in the chat messages database. As new messages arrive, they are appended to the local messages state, ensuring real-time display without refreshing.
Message Deletion:
Senders can hover over their messages to see a delete option, which prompts for confirmation using an alert. Upon approval, the message gets deleted from both the UI and the database.
UI Components:
The chat interface is streamlined with input fields for typing messages and a button for sending. It also provides hover-based UI actions, like deleting messages.
React combined with Supabase offers a potent duo for crafting real-time chat apps. The modular nature of the code presented means you can readily incorporate more features if needed.
View the entire code on GitHub.
For a more hands-on approach, follow the guide below.
This section remains the same as the original content unless you need updates here too.
First, clone the project using:
bashgit clone https://github.com/owolfdev/chat-app-test
Navigate to the project directory:
bashcd [PROJECT_DIRECTORY_NAME]
Install the dependencies:
bashnpm install
To create a new Git repository, first remove the existing .git directory:
bashrm -rf .git
Now, initialize a new Git repository:
bashgit init
Create a new repository on GitHub (or your preferred Git hosting service). Then link your local repository to this new remote repository and push your code:
bashgit remote add origin [URL_TO_YOUR_NEW_GIT_REPO]
git add .
git commit -m "Initial commit"
git push -u origin master
Navigate to Supabase and sign up for a free account.
Once signed in, create a new project.
Supabase will provide you with a supabaseUrl and supabaseKey. Keep these credentials safe; you'll need them to interact with your backend from your React application.
In the Supabase dashboard, go to the Table Editor.
Click New Table and create a table named chat_messages. This table could have columns like chat_id, content, id, sender_id, sent_at, and updated_at.
Create a table named profiles with columns user_id and avatar. Store the user's avatar image url in the avatar column.
In the Supabase dashboard, navigate to the Authentication section.
Add users.
Add profiles for each user in the profiles table.
Configure your preferred login method(s) (e.g., email/password, third-party providers).
In your React app, integrate Supabase's authentication module. Supabase provides React-specific libraries to make this step straightforward.
Examine the source code for some guidance. You may well need to do some of reading on supabase authentication to get these steps right.
Install the necessary Supabase libraries:
bashnpm install @supabase/supabase-js
In your React app, initialize Supabase using the supabaseUrl and supabaseKey you obtained earlier.
First, install the Vercel CLI:
bashnpm install -g vercel
Navigate to your project's root directory and run:
bashvercel
Follow the prompts provided by the Vercel CLI.
Once deployment is complete, Vercel will provide you with a live URL to access your application.
Consider securing your Supabase functions with appropriate access controls, especially if dealing with sensitive data.