DEV Community

Cover image for Svelte & Supabase Real-time Chat app
Ahmd Talat
Ahmd Talat

Posted on

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/

Top comments (4)

Collapse
 
creativemacmac profile image
creativemacmac

So Just got hooked on svelte so of course had to read this thanks a mil for showing us what we can do with svelte and supabase🤩

Collapse
 
alisanad profile image
Ali Sanad

Great work Ahmed, keep going.

Collapse
 
ginanjar1018 profile image
Ginanjar

why when I enter a message, the message does not appear

Collapse
 
thareekanvar profile image
Thareek Anvar M

Dude .. can i know how can i handel the message read count ??