Skip to content

Instantly share code, notes, and snippets.

@bookercodes
Created August 19, 2017 13:28
Show Gist options
  • Save bookercodes/6216cc9c15a682611cbfcba05b517c00 to your computer and use it in GitHub Desktop.
Save bookercodes/6216cc9c15a682611cbfcba05b517c00 to your computer and use it in GitHub Desktop.
const Pusher = require('pusher')
const uuid = require('uuid/v1')
const ngrok = require('ngrok')
var pusher = new Pusher({
appId: '385685',
key: '955f28f383404945239b',
secret: 'c58baaf698167224517f',
cluster: 'eu',
encrypted: true
})
ngrok.connect(8080, (err, url) => {
console.log(url)
})
const app = express()
app.engine('handlebars', exphbs({ defaultLayout: 'main' }))
app.set('view engine', 'handlebars')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.get('/', (req, res) => {
res.render('home', { layout: false, initialText: 'Hello' })
})
let whoIsTyping = null
let timer
const processEvent = (timestamp, event) => {
const broadcast = function() {
whoIsTyping = null
pusher.trigger('typing-events', 'update-whoIsTyping', {
whoIsTyping
})
}
if (whoIsTyping === event.user_id) {
clearTimeout(timer)
timer = setTimeout(broadcast, 2000)
return
}
whoIsTyping = event.user_id
pusher.trigger('typing-events', 'update-whoIsTyping', {
whoIsTyping
})
timer = setTimeout(broadcast, 2000)
console.log('timer', timer)
}
const findOnInputEvents = events =>
events.filter(event => event.event === 'client-oninput')
app.post('/client_event', function(req, res) {
var timestamp = req.body.time_ms
var events = req.body.events
events.forEach(event => processEvent(timestamp, event))
res.sendStatus(200)
})
app.post('/pusher/auth', function(req, res) {
console.log(req.body)
const user_id = uuid()
console.log(user_id)
var presenceData = {
user_id
}
const auth = pusher.authenticate(
req.body.socket_id,
req.body.channel_name,
presenceData
)
res.send(auth)
})
app.listen(8080)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment