Last active
February 3, 2024 19:50
-
-
Save igortrinidad/dd9b8fdabe852a15013c51660efcc21f to your computer and use it in GitHub Desktop.
How to start a Socket.io service in Adonis 6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() {} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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