-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
51 lines (42 loc) · 1.37 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import express from "express"
import expressWs from "express-ws"
import next from 'next'
import { parse } from 'url'
import * as db from './db.js'
import { connectToRedis, publish, subscribe } from './pubsub.js'
const { app, getWss } = expressWs(express())
const port = 3000
async function broadcast(clients = null) {
const clips = await db.query('SELECT name, text FROM clips ORDER BY id')
for (const wsClient of clients || getWss().clients) {
try { wsClient.send(JSON.stringify(clips.rows)) } catch { };
}
}
// Define web socket route
app.ws('/websocket', async ws => {
// update client on a best effort basis
try {
broadcast([ws])
} catch (error) {
console.error(error)
}
// We don’t expect any messages on websocket, but log any ones we do get.
ws.on('message', console.log)
})
const nextApp = next({ dev: process.env.NODE_ENV !== "production" })
nextApp.prepare().then(async () => {
await connectToRedis()
subscribe(broadcast);
// route requests to Next.js; publish timestamp on PUT and DELETE
let handler = nextApp.getRequestHandler()
app.use(async (request, response) => {
await handler(request, response, parse(request.url, true))
if (request.method === 'PUT' || request.method === 'DELETE') {
await publish()
}
})
})
// Start the server
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})