Skip to content

Instantly share code, notes, and snippets.

@igortrinidad
Last active February 3, 2024 19:50
Show Gist options
  • Save igortrinidad/dd9b8fdabe852a15013c51660efcc21f to your computer and use it in GitHub Desktop.
Save igortrinidad/dd9b8fdabe852a15013c51660efcc21f to your computer and use it in GitHub Desktop.
How to start a Socket.io service in Adonis 6
// Remember to create the provider and register it in adonisrc.ts
import type { ApplicationService } from '@adonisjs/core/types'
export default class SocketProvider {
constructor(protected app: ApplicationService) {}
/**
* Register bindings to the container
*/
register() {}
/**
* The container bindings have booted
*/
async boot() {}
/**
* The application has been booted
*/
async start() {
}
/**
* The process has been started
*/
async ready() {
try {
const moduleImport = await import('../app/services/Socket/index.ts')
const Socket = moduleImport.default
await Socket.boot()
console.log('SocketProvider started')
} catch (error) {
console.log('Error in SocketProvider)')
console.error(error)
}
}
/**
* Preparing to shutdown the app
*/
async shutdown() {}
}
import type { ApplicationService } from '@adonisjs/core/types'
export default class SocketProvider {
constructor(protected app: ApplicationService) {}
/**
* Register bindings to the container
*/
register() {}
/**
* The container bindings have booted
*/
async boot() {}
/**
* The application has been booted
*/
async start() {
}
/**
* The process has been started
*/
async ready() {
if(this.app.getEnvironment() === 'web') {
try {
const moduleImport = await import('../app/services/Socket/index.ts')
const Socket = moduleImport.default
await Socket.boot()
console.log('SocketProvider started')
} catch (error) {
console.log('Error in SocketProvider)')
console.error(error)
}
}
}
/**
* Preparing to shutdown the app
*/
async shutdown() {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment