2.11.5 • Published 4 months ago

@supabase/realtime-js v2.11.5

Weekly downloads
2,365
License
MIT
Repository
github
Last release
4 months ago

Overview

This client enables you to use the following Supabase Realtime's features:

  • Broadcast: send ephemeral messages from client to clients with minimal latency. Use cases include sharing cursor positions between users.
  • Presence: track and synchronize shared state across clients with the help of CRDTs. Use cases include tracking which users are currently viewing a specific webpage.
  • Postgres Change Data Capture (CDC): listen for changes in your PostgreSQL database and send them to clients.

Usage

Installing the Package

npm install @supabase/realtime-js

Creating a Channel

import { RealtimeClient } from '@supabase/realtime-js'

const client = new RealtimeClient(REALTIME_URL, {
  params: {
    apikey: API_KEY
  },
})

const channel = client.channel('test-channel', {})

channel.subscribe((status, err) => {
  if (status === 'SUBSCRIBED') {
    console.log('Connected!')
  }

  if (status === 'CHANNEL_ERROR') {
    console.log(`There was an error subscribing to channel: ${err.message}`)
  }

  if (status === 'TIMED_OUT') {
    console.log('Realtime server did not respond in time.')
  }

  if (status === 'CLOSED') {
    console.log('Realtime channel was unexpectedly closed.')
  }
})

Notes:

  • REALTIME_URL is 'ws://localhost:4000/socket' when developing locally and 'wss://<project_ref>.supabase.co/realtime/v1' when connecting to your Supabase project.
  • API_KEY is a JWT whose claims must contain exp and role (existing database role).
  • Channel name can be any string.

Broadcast

Your client can send and receive messages based on the event.

// Setup...

const channel = client.channel('broadcast-test', { broadcast: { ack: false, self: false } })

channel.on('broadcast', { event: 'some-event' }, (payload) =>
  console.log(payload)
)

channel.subscribe(async (status) => {
  if (status === 'SUBSCRIBED') {
    // Send message to other clients listening to 'broadcast-test' channel
    await channel.send({
      type: 'broadcast',
      event: 'some-event',
      payload: { hello: 'world' },
    })
  }
})

Notes:

  • Setting ack to true means that the channel.send promise will resolve once server replies with acknowledgement that it received the broadcast message request.
  • Setting self to true means that the client will receive the broadcast message it sent out.
  • Setting private to true means that the client will use RLS to determine if the user can connect or not to a given channel.

Presence

Your client can track and sync state that's stored in the channel.

// Setup...

const channel = client.channel(
  'presence-test',
  {
    config: {
      presence: {
        key: ''
      }
    }
  }
)

channel.on('presence', { event: 'sync' }, () => {
  console.log('Online users: ', channel.presenceState())
})

channel.on('presence', { event: 'join' }, ({ newPresences }) => {
  console.log('New users have joined: ', newPresences)
})

channel.on('presence', { event: 'leave' }, ({ leftPresences }) => {
  console.log('Users have left: ', leftPresences)
})

channel.subscribe(async (status) => {
  if (status === 'SUBSCRIBED') {
    const status = await channel.track({ 'user_id': 1 })
    console.log(status)
  }
})

Postgres CDC

Receive database changes on the client.

// Setup...

const channel = client.channel('db-changes')

channel.on('postgres_changes', { event: '*', schema: 'public' }, (payload) => {
  console.log('All changes in public schema: ', payload)
})

channel.on('postgres_changes', { event: 'INSERT', schema: 'public', table: 'messages' }, (payload) => {
  console.log('All inserts in messages table: ', payload)
})

channel.on('postgres_changes', { event: 'UPDATE', schema: 'public', table: 'users', filter: 'username=eq.Realtime' }, (payload) => {
  console.log('All updates on users table when username is Realtime: ', payload)
})

channel.subscribe(async (status) => {
  if (status === 'SUBSCRIBED') {
    console.log('Ready to receive database changes!')
  }
})

Get All Channels

You can see all the channels that your client has instantiatied.

// Setup...

client.getChannels()

Cleanup

It is highly recommended that you clean up your channels after you're done with them.

  • Remove a single channel
// Setup...

const channel = client.channel('some-channel-to-remove')

channel.subscribe()

client.removeChannel(channel)
  • Remove all channels
// Setup...

const channel1 = client.channel('a-channel-to-remove')
const channel2 = client.channel('another-channel-to-remove')

channel1.subscribe()
channel2.subscribe()

client.removeAllChannels()

Credits

This repo draws heavily from phoenix-js.

License

MIT.

2.11.4

4 months ago

2.11.5

4 months ago

2.11.0

6 months ago

2.11.1

6 months ago

2.11.2

6 months ago

2.11.3

6 months ago

2.10.9

7 months ago

2.10.8

7 months ago

2.11.4-next.1

6 months ago

2.10.7-next.1

8 months ago

2.10.7

8 months ago

2.10.0-next.8

1 year ago

2.10.1

12 months ago

2.10.2

12 months ago

2.10.0

12 months ago

2.10.5

9 months ago

2.10.6

9 months ago

2.10.3

12 months ago

2.10.4

11 months ago

2.10.5-next.2

9 months ago

2.10.5-next.1

9 months ago

2.9.5

1 year ago

2.10.0-next.7

1 year ago

2.9.4

1 year ago

2.10.0-next.6

1 year ago

2.10.0-next.3

1 year ago

2.10.0-next.4

1 year ago

2.10.0-next.5

1 year ago

2.10.0-next.2

1 year ago

2.10.0-next.1

1 year ago

2.9.2

1 year ago

2.9.3

1 year ago

2.9.1

1 year ago

2.9.0

2 years ago

2.8.1

2 years ago

2.8.0

2 years ago

2.7.4

2 years ago

2.8.3

2 years ago

2.8.2

2 years ago

2.8.4

2 years ago

2.7.3

2 years ago

2.7.0

2 years ago

2.7.2

2 years ago

2.7.1

2 years ago

2.6.0

2 years ago

2.5.0

2 years ago

2.2.0

2 years ago

2.4.0

2 years ago

2.3.0

2 years ago

2.3.1

2 years ago

2.0.0

3 years ago

2.0.0-rc.6

3 years ago

2.0.0-rc.7

3 years ago

2.1.0

3 years ago

1.7.5

3 years ago

2.0.0-rc.4

3 years ago

2.0.0-rc.5

3 years ago

2.0.0-rc.2

3 years ago

2.0.0-rc.3

3 years ago

1.8.0-next.13

3 years ago

1.8.0-next.12

3 years ago

1.8.0-next.15

3 years ago

1.8.0-next.14

3 years ago

1.8.0-next.11

3 years ago

1.8.0-next.10

3 years ago

1.8.0-next.17

3 years ago

1.8.0-next.16

3 years ago

1.7.4

3 years ago

2.0.0-rc.1

3 years ago

1.7.3

3 years ago

1.8.0-next.8

3 years ago

1.8.0-next.7

3 years ago

1.8.0-next.9

3 years ago

1.8.0-next.4

3 years ago

1.8.0-next.3

3 years ago

1.8.0-next.6

3 years ago

1.8.0-next.5

3 years ago

1.8.0-next.2

3 years ago

1.8.0-next.1

3 years ago

1.6.2

3 years ago

1.6.1

3 years ago

1.7.2

3 years ago

1.7.1

3 years ago

1.7.0

3 years ago

1.4.6

3 years ago

1.4.5

3 years ago

1.4.4

3 years ago

1.4.3

3 years ago

1.6.0

3 years ago

1.5.1

3 years ago

1.4.2

3 years ago

1.5.0

3 years ago

1.4.0

3 years ago

1.3.6

3 years ago

1.3.5

3 years ago

1.3.4

3 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.2.1

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.9.0

5 years ago

0.1.8

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

6 years ago

0.1.2

6 years ago

0.1.0

6 years ago