Skip to content
This repository has been archived by the owner on Jun 22, 2023. It is now read-only.

Commit

Permalink
Update Dependencies & Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
moonglum committed Jun 1, 2020
1 parent d7ac4c9 commit be1be94
Show file tree
Hide file tree
Showing 6 changed files with 1,040 additions and 928 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3"
services:
redis:
image: "redis:5.0-rc3"
image: "redis:6.0"
restart: always
chat:
build: .
Expand Down
46 changes: 23 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
let path = require('path')
const path = require('path')

let express = require('express')
let app = express()
let bodyParser = require('body-parser')
let cons = require('consolidate')
let faker = require('faker')
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const cons = require('consolidate')
const faker = require('faker')

let Redis = require('ioredis')
const Redis = require('ioredis')
Redis.Command.setArgumentTransformer('xadd', xaddArgumentTransformer)
Redis.Command.setReplyTransformer('xread', xreadResultParser)

Expand All @@ -16,13 +16,13 @@ app.set('view engine', 'mustache')
app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.static(path.resolve('public')))

let producer = new Redis('redis://redis:6379')
const producer = new Redis('redis://redis:6379')

// 10 is an arbitrary number
app.get('/', async function (req, res) {
// TODO: Argument transformer and result parser
// This is a weird way of getting the last 10 messages
let user = faker.name.findName()
const user = faker.name.findName()
let messages = await producer.xrevrange('messages', '+', '-', 'COUNT', 10)
messages = messages.reverse()

Expand All @@ -38,7 +38,7 @@ app.get('/', async function (req, res) {
})

app.post('/messages', function (req, res) {
let { message, user } = req.body
const { message, user } = req.body
producer.xadd('messages', {
id: '*', // The * means: Determine the ID yourself
text: message,
Expand All @@ -49,12 +49,12 @@ app.post('/messages', function (req, res) {

// This parameter is written into the template by Node
app.get('/update-stream', async function (req, res) {
let consumer = new Redis('redis://redis:6379')
const consumer = new Redis('redis://redis:6379')

res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
Connection: 'keep-alive'
})
res.write('\n')

Expand All @@ -68,7 +68,7 @@ app.get('/update-stream', async function (req, res) {
// TODO: Build an argument parser for xread?
// The timeout is set to 0 to wait indefinitely. This probably has to be set to something different
// so we can handle disconnecting clients
let { messages } = await consumer.xread('block', 0, 'STREAMS', 'messages', lastId)
const { messages } = await consumer.xread('block', 0, 'STREAMS', 'messages', lastId)
messages.forEach(message => {
lastId = message.id
res.write(`id: ${message.id}\n`)
Expand All @@ -83,12 +83,12 @@ console.log('App listening on 8000')

// Clean up these methods
function xreadResultParser (results) {
let x = {}
const x = {}
results.forEach(result => {
let y = []
const y = []
result[1].forEach(message => {
let [id, foo] = message
let parsedMessage = arrayToObject(foo)
const [id, foo] = message
const parsedMessage = arrayToObject(foo)
parsedMessage.id = id
y.push(parsedMessage)
})
Expand All @@ -102,13 +102,13 @@ function xaddArgumentTransformer (args) {
return args
}

let result = []
const result = []

let [stream, kv] = args
const [stream, kv] = args
result.push(stream)
result.push(kv.id) // default to *?
delete kv.id
for (let key in kv) {
for (const key in kv) {
result.push(key)
result.push(kv[key])
}
Expand All @@ -117,10 +117,10 @@ function xaddArgumentTransformer (args) {
}

function arrayToObject (arr) {
let result = {}
const result = {}
for (let i = 0; i < arr.length; i += 2) {
let key = arr[i]
let value = arr[i + 1]
const key = arr[i]
const value = arr[i + 1]
result[key] = value
}
return result
Expand Down
Loading

0 comments on commit be1be94

Please sign in to comment.