Svelte & Supabase Real-time Chat app

ahmdtalat

Ahmd Talat

Posted on February 20, 2022

Svelte & Supabase Real-time Chat app

Welcome, this tutorial shows how to build a simple chat app with Svelte.js and supabase.

Demo

Getting started

Step 1: Create a new project

$ yarn create vite my-svelte-app --template svelte-ts

$ cd my-svelte-app
$ yarn install
Enter fullscreen mode Exit fullscreen mode

now, open your app with vscode & you should have a structure like this:
Application folder structure

step 2: Go to supabase & create an account

  • create a new project to get your app's credentials

supabaseUrl | supabaseKey
once you have them, Create .env file and store the values

VITE_PUBLIC_SUPABASE_URL="https://<your project id>.supabase.co"
VITE_PUBLIC_SUPABASE_KEY="your public anon key"

Enter fullscreen mode Exit fullscreen mode
  • we need to create the Messages table, you can do that by supabase UI or with a sql command
CREATE TABLE messages (
  id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
  username VARCHAR NOT NULL,
  text TEXT NOT NULL,
  timestamp timestamp default now() NOT NULL
);
Enter fullscreen mode Exit fullscreen mode

db msgs table

step 3: Create the store

once you have your API keys, now let's create a store.

Store in svelte is an object that holds you app's state

  • First, we need to install one more package to connect to supabase
yarn add @supabase/supabase-js 
Enter fullscreen mode Exit fullscreen mode
  • create a store file your_app_dir/src/store.ts
  • next, we will initialize a supabase client & a writable store

supabase code initialization

  • supabase gives us a function that receive a notification every time an auth event happens. by default it stores the user auth token in the localStorage & this function with run when you refresh you app.
supabase.auth.onAuthStateChange((event, session) => {
  if (event == 'SIGNED_IN') {
    store.update((oldStore) => { // updating the store if there is a token stored
      return {
        ...oldStore,
        user: session.user
      }
    })
  } else if (event == 'SIGNED_OUT') {
    store.set(defaultStore) // set the store to the default value
  }
})
Enter fullscreen mode Exit fullscreen mode
  • Finally, we export a default object with the functions that we will use in other parts of the app. authentication & chat functions

Step 4: Last step

Now, we can create the components that we need for our little chat app. There are two main components, a Chat component that will be rendered when a user login, else a Login component will take place.

one more thing, I'd like to explain the Realtime part. it's pretty straight forward. Supabase gives us a subscribe function that we can use to listen for database events like ['*','INSERT','UPDATE','DELETE'].

Listen for new msgs

I hope you find this easy & helpful, and maybe you can build on top of it for your next app. Enjoy!! :)

Repo: https://github.com/ahmdtalat/svelte-supabase-chat

you can check the docs here Svelte Supabase

cover: https://undraw.co/

💖 💪 🙅 🚩
ahmdtalat
Ahmd Talat

Posted on February 20, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Svelte & Supabase Real-time Chat app
supabase Svelte & Supabase Real-time Chat app

February 20, 2022