Simple chat app with WebSockets ( express-ws ) where both front and back are in single Nuxt.js project.
This project shows how to build simple application with WebSockets in Nuxt.js.
# install dependencies
$ npm run install
# serve with hot reload at localhost:3000
$ npm run dev
# build for production and launch server
$ npm run build
$ npm run start
For detailed explanation on how things work, check out Nuxt.js docs.
Most important part is opening websocket in the server/index.js:
...
var expressWs = require('express-ws')(app)
app.ws('/chat', function(ws, req) {
ws.on('message', function(msg) {
expressWs.getWss().clients.forEach(client => client.send(msg))
})
})
...
Rest is in the pages/index.vue.
- Run application.
npm i
npm run dev
-
Open http://localhost:3000/ in two browsers.
-
Change nickname at least in one of pages with chat.
-
Enjoy :-)