Nuxt/examples/with-sockets/io/index.js
Jonas Galvez 3f1d634fb7 Consistent parens in arrow functions (#3630)
* Minor consistency enhancements

* Arrow parenthesis consistency

* Change linting rule

* Fix typo

* Update .eslintrc.js to only require parens for blocks

* Update style according to brace-only suggestion

* Remove --fix from lint

* Tweak no-loading time (failing test)

* Tweak no-loading time (failing test) (2)

* Tweak no-loading time (failing test) (3)

* Tweak no-loading time (failing test) (4)

* Tweak no-loading time (failing test) (5)
2018-08-06 02:12:44 +02:00

25 lines
733 B
JavaScript

import http from 'http'
import socketIO from 'socket.io'
const server = http.createServer(this.nuxt.renderer.app)
const io = socketIO(server)
export default function () {
// overwrite nuxt.listen()
this.nuxt.listen = (port, host) => new Promise(resolve => server.listen(port || 3000, host || 'localhost', resolve))
// close this server on 'close' event
this.nuxt.hook('close', () => new Promise(server.close))
// Add socket.io events
let messages = []
io.on('connection', (socket) => {
socket.on('last-messages', function (fn) {
fn(messages.slice(-50))
})
socket.on('send-message', function (message) {
messages.push(message)
socket.broadcast.emit('new-message', message)
})
})
}