adonisjs / core

AdonisJS is a TypeScript-first web framework for building web apps and API servers. It comes with support for testing, modern tooling, an ecosystem of official packages, and more.
https://adonisjs.com
MIT License
16.92k stars 640 forks source link

Issue with Socket.io not running with Adonis Server Instance #4148

Closed roqkabel closed 1 year ago

roqkabel commented 1 year ago

Problem statement

I am using AdonisJs with socket.io, and I followed the documentation for using with socket.io

When on the Ws Service Class, using this code provided by the docs does not instantiate the socket and always returns 404 not found /socket.io/ But when I change the socket to use a different port, I was able to connect to the socket.

Actual Results

When using this code works and I am able to connect to socket.io from the client using ws://localhost:3000 meanwhile app server is running on port 3333

    this.io = new Server(3000, {
      path: '/socket.io',
    })

Expect Results

From the docs, this code should allow a socket connection.

    this.io = new Server(AdonisServer.instance!, {
      path: '/socket.io',
    })

Package version

AdonisJs Version 5

Node.js and npm version

Node v18.15.0 NPM 9.5.0

Sample Code (to reproduce the issue)

  public boot() {
    /**
     * Ignore multiple calls to the boot method
     */
    if (this.booted) {
      return
    }

    this.booted = true

    this.io = new Server(AdonisServer.instance!, {
      path: '/socket.io',
    })
  }
roqkabel commented 1 year ago

@aikrom

Can you be able to help with this issue?

Would be glad you check this out.

aikrom commented 1 year ago

It looks like you didn't boot socket.io.

The code below must be in your provider:

import { ApplicationContract } from '@ioc:Adonis/Core/Application'

export default class AppProvider {
  constructor(protected app: ApplicationContract) {}

  public async ready() {
    if (this.app.environment === 'web') {
      await import('../start/socket')
    }
  }
}

If it doesn't help, create a repository to reproduce the error

roqkabel commented 1 year ago

okay, let me try this.