Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Making realtime work on supabase-js

Paul Copplestone822675dfb840
diff --git a/.gitignore b/.gitignore
index 2b7642397d0..1883cd20de5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,6 +3,7 @@ node_modules
.next
out
.docz
+tmp
coverage
.nyc_output
diff --git a/libraries/supabase-js/README.md b/libraries/supabase-js/README.md
index 805dc7214bf..3b14c95c31a 100644
--- a/libraries/supabase-js/README.md
+++ b/libraries/supabase-js/README.md
@@ -1,6 +1,6 @@
# Supabase JS
-# Commands
+## Commands
- `npm run clean` - Remove `lib/` directory
- `npm test` - Run tests with linting and coverage results.
- `npm test:only` - Run tests without linting or coverage.
@@ -12,3 +12,11 @@
- `npm run build` - Babel will transpile ES6 => ES5 and minify the code.
- `npm run prepublish` - Hook for npm. Do all the checks before publishing your module.
+
+## Publishing
+
+Bump the semver.
+
+```sh
+npm publish --access=public
+```
\ No newline at end of file
diff --git a/libraries/supabase-js/package.json b/libraries/supabase-js/package.json
index 37d608ddd8a..b4f04afd8bd 100644
--- a/libraries/supabase-js/package.json
+++ b/libraries/supabase-js/package.json
@@ -1,6 +1,6 @@
{
"name": "@supabase/supabase-js",
- "version": "0.1.0",
+ "version": "0.1.05",
"description": "Supabase Realtime API",
"main": "./lib/index.js",
"scripts": {
diff --git a/libraries/supabase-js/src/Base.js b/libraries/supabase-js/src/Base.js
index 1e39de5c798..544f19b442c 100644
--- a/libraries/supabase-js/src/Base.js
+++ b/libraries/supabase-js/src/Base.js
@@ -1,13 +1,14 @@
-import { Socket } from 'phoenix-channels'
+const {Socket} = require('phoenix-channels')
import BaseRequest from './BaseRequest'
class Base {
- constructor(tableName, restUrl, apiSocket, schema, uuid) {
+ constructor(tableName, restUrl, realtimeUrl, schema, apikey, uuid) {
this.tableName = tableName
this.restUrl = restUrl
- this.apiSocket = apiSocket
+ this.realtimeUrl = realtimeUrl
this.schema = schema
this.uuid = uuid
+ this.apikey = apikey
this.socket = null
this.channel = null
@@ -17,23 +18,28 @@ class Base {
}
createListener() {
- this.socket = new Socket(this.apiSocket)
- this.channel = this.socket.channel(`realtime:${this.schema}:${this.tableName}`)
+ let socketUrl = `${this.realtimeUrl}`
+ let channel = `realtime:${this.schema}:${this.tableName}`
+ this.socket = new Socket(socketUrl)
+ this.channel = this.socket.channel('realtime:*') // @TODO:
+
+ // @TODO: remove
+ // Example local config
+ // this.socket = new Socket(`ws://localhost:80/socket`)
+ // this.channel = this.socket.channel('realtime:*')
this.socket.onOpen(() => {
- console.log('Socket connected')
+ console.log('REALTIME CONNECTED')
})
this.socket.onClose(() => {
- console.log('Socket disconnected')
+ console.log('REALTIME DISCONNECTED')
})
}
on(eventType, callbackFunction) {
if (this.socket == null) this.createListener()
-
var ref = this.channel.on(eventType, callbackFunction)
this.listeners[eventType] = ref
-
return this
}
@@ -60,7 +66,7 @@ class Base {
}
request(method) {
- let path = `${this.restUrl}/${this.tableName}`
+ let path = `${this.restUrl}/${this.tableName}?apikey=${this.apikey}`
return new BaseRequest(method, path)
}
@@ -74,7 +80,6 @@ class Base {
// loop through this.queryFilters
this.queryFilters.forEach(queryFilter => {
-
let columnName = queryFilter.columnName
let operator = queryFilter.operator
let criteria = queryFilter.criteria
diff --git a/libraries/supabase-js/src/index.js b/libraries/supabase-js/src/index.js
index 3e89e3d1bf3..3e9b64205b4 100644
--- a/libraries/supabase-js/src/index.js
+++ b/libraries/supabase-js/src/index.js
@@ -3,35 +3,34 @@ import Base from './Base'
class SupabaseClient {
constructor(supabaseUrl, supabaseKey, options = {}) {
- this.supabaseUrl = supabaseUrl
- this.supabaseKey = supabaseKey
-
+ this.supabaseUrl = null
+ this.supabaseKey = null
this.restUrl = null
- this.apiSocket = null
- this.schema = null
+ this.realtimeUrl = null
+ this.defaultSchema = 'public'
this.subscriptions = {}
this.authenticate(supabaseUrl, supabaseKey)
}
authenticate(supabaseUrl, supabaseKey){
- // do something to utilise parameters and receive
- // the restUrl, apiSocket and schema
-
- var restUrl = ''
- var apiSocket = ''
- var schema = ''
-
- // harcoded
- this.restUrl = 'http://localhost:3000'
- this.apiSocket = 'ws://localhost:4000/socket'
- this.schema = 'public'
+ this.supabaseUrl = supabaseUrl
+ this.supabaseKey = supabaseKey
+ this.restUrl = supabaseUrl + '/rest/v1'
+ this.realtimeUrl = `${supabaseUrl}/socket`.replace('http', 'ws')
}
from(tableName){
let identifier = uuid()
- this.subscriptions[identifier] = new Base(tableName, this.restUrl, this.apiSocket, this.schema, identifier)
+ this.subscriptions[identifier] = new Base(
+ tableName,
+ this.restUrl,
+ this.realtimeUrl,
+ this.defaultSchema,
+ this.supabaseKey,
+ identifier
+ )
return this.subscriptions[identifier]
}