Created
May 31, 2020 12:14
-
-
Save retendo/3e2ddb90a29dfd0f5a2ab81729c7ed29 to your computer and use it in GitHub Desktop.
Supabase node.js proxy server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Socket } = require('@supabase/realtime-js'); | |
const WebSocket = require('ws'); | |
const wss = new WebSocket.Server({ port: 3000 }); | |
wss.on('connection', function connection(ws) { | |
ws.send('Connection established.'); | |
}); | |
const socket = new Socket(process.env.REALTIME_URL || 'http://localhost:4000/socket') | |
socket.connect() | |
socket.channel('realtime:public') | |
.join() | |
.channel | |
.on('*', payload => { | |
wss.clients.forEach(function each(client) { | |
if (client.readyState === WebSocket.OPEN) { | |
client.send(JSON.stringify({ | |
"type": payload.table, | |
"operation": payload.type, | |
"object": payload.record != null ? payload.record : payload.old_record | |
})); | |
} | |
}); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "realtime-proxy", | |
"scripts": { | |
"dev": "node index.js" | |
}, | |
"version": "1.0.0", | |
"main": "index.js", | |
"license": "MIT", | |
"dependencies": { | |
"@supabase/realtime-js": "^0.9.0", | |
"bufferutil": "^4.0.1", | |
"express": "^4.17.1", | |
"utf-8-validate": "^5.0.2", | |
"ws": "^7.3.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
docker rm -f realtime || true | |
docker run -d -p 4000:4000 --name=realtime \ | |
-e DB_HOST='docker.for.mac.host.internal' \ | |
-e DB_NAME="$POSTGRES_DB" \ | |
-e DB_USER='postgres' \ | |
-e DB_PASSWORD='' \ | |
-e DB_PORT="$POSTGRES_PORT" \ | |
-e PORT=4000 \ | |
-e HOSTNAME='localhost' \ | |
-e SECRET_KEY_BASE='SOMETHING_SUPER_SECRET' \ | |
supabase/realtime |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run instructions:
Disclaimer: this works for me on Mac
To test: