Skip to content

Latest commit

 

History

History
60 lines (40 loc) · 1.66 KB

README.md

File metadata and controls

60 lines (40 loc) · 1.66 KB

@softmila/adonisjs-socketio


npm-image license-image

Introduction

AdonisJS-SocketIO provides JavaScript API to implementation socketio in AdonisJS applications.

Installation

node ace add @softmila/adonisjs-socketio

Usage

Create a service to handle event from socketio

// SocketIoServiceHandler.ts
import { OnMessage, OnceMessage, OnAnyMessage} from '@softmila/adonisjs-socketio'

export default class SocketIoServiceHandler {

  @OnMessage('on_message')
  async onMessageHandler(socket: Socket, data: any){
    console.log('Data:::', data)
    const io = await app.container.make('io')
    socket.emit('on_response', {data "ok"})
  }

  @OnceMessage('once_message')
  async onceMessageHandler(socket: Socket, data: any){
    console.log('Data:', data)
  }

  @OnAnyMessage()
  async onAnyMessageHandler(socket: Socket, data: any){
    console.log('Data:', data)
  }
}

io is avalaible in HttpContext and ContainerBindings .

Note: Services that handle socketio event can't inject dependency that depends HttpContext.

License

Adonisjs-SocketIO is open-sourced software licensed under the MIT license.