A
A
Alexander Ivanov2019-08-21 16:29:42
Node.js
Alexander Ivanov, 2019-08-21 16:29:42

Why doesn't socket.io return connection on nuxt always 404?

so in nuxt I connect the plugin

plugins: [
    { src: '~/plugins/socket.io.js', ssr: false }
  ],

Errors are thrown into the console:

vendors.app.js:2332 GET localhost:3000/socket.io/?EIO=3&transport=polling&... 404 (OK)

When I connect the plugin like this, it still does not plow:
// plugins/socket.io.js
import Vue from 'vue';
import io from 'socket.io-client';
import VueSocketIOExt from 'vue-socket.io-extended';
 
const socket = io('http://localhost:3000');
 
export default ({ store }) => {
  Vue.use(VueSocketIOExt, socket, { store });
}

serve.js
import Koa from 'koa'
import { Nuxt, Builder } from 'nuxt'
import * as _io from 'socket.io'
import http from 'http'
import rootRouter from './routes'

const app = new Koa(),
      host = process.env.HOST || '127.0.0.1',
      port = process.env.PORT || 3000,
      server = http.createServer(app.callback()),
      io = _io(server)

io.on('connection', socket => {
  console.log('io Connected')
  socket.emit('newMessage', {
    text: 'WHAT'
  })
})

var start = async function() {
  const config = require('../nuxt.config.js')
  config.dev = !(app.env === 'production')
  const nuxt = new Nuxt(config)
  if (config.dev) {
    const builder = new Builder(nuxt)
    await builder.build()
  }
  app.use(rootRouter.routes()).use(ctx => {
    ctx.status = 200
    ctx.respond = false
    ctx.req.ctx = ctx 
    nuxt.render(ctx.req, ctx.res)
  })
  app.listen(port, host)
  console.log('Server listening on ' + host + ':' + port) 
}
start()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ivanov, 2019-08-21
@cimonlebedev

I was listening through the app and not through the server... For some reason it didn't bother me last time..
server.listen(port, host)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question