9.0.3 • Published 2 years ago

@appolo/socket v9.0.3

Weekly downloads
59
License
MIT
Repository
-
Last release
2 years ago

Socket module for appolo build with socket.io

Installation

npm i @appolo/scoket

Options

keyDescriptionTypeDefault
idSocketProvider injection idstringsocketProvider
autotrue to auto initialize socket listen eventsbooleantrue
redisredis connection for sockets syncstringnull
socketsocket optionsobject{"transports": ["websocket"]}

in config/modules/all.ts

import {ScoketModule} from '@appolo/socket';

export = async function (app: App) {
   await app.module(new ScoketModule({redis:"redis://redis-connection-string"}));
}

SocketController

will be created on socket new connection and holds the socket instance. must be inherit from SocketController and defined using @socket.

you can define custom namespace using @socket("someNamespace") default to /.

you subscribe to socket events using @action('someEvent') the return object from the action will be passed to socket callback id exists promises also supported.

import {action, socket, SocketController} from "@appolo/scoket";

@socket()
export class MySocketController extends SocketController {

    @action("someAction")
    public asyc test(name: string) {
        let someData  = await doSomeThingAsync();

        return {arg:name,someData}
    }
}

socket client

import * as io from 'socket.io-client';

let socket = io("http://localhost:8080")

socket.emit("someAction", "working" ({arg})=>{
    console.log(arg) // working
})

Hooks

  • onInitialized - called when socket initialized
  • onDisconnected - called when socket disconnected
  • onConnected - called when socket connected
import {action, socket, SocketController} from "@appolo/scoket";

@socket()
export class MySocketController extends SocketController {

    @action("someAction")
    public test(name: string) {
        return {arg:name}
    }

    onDisconnected(){
        // do something
    }
}

Methods

  • get socket(): socketIo.Socket - return socket instance
  • get id(): string - return socket id
  • send(event: string, data: any) - emit socket event

Middleware

Middleware can be used before socket connect

import {IMiddleware} from "@appolo/scoket";

@define()
export class TokenMiddleware implements IMiddleware {

    run(socket: socketIo.Socket, next: NextFn) {
        if (socket.handshake.query.token == "1") {
            socket.request.user = 1;
            next()
        } else {
            next(new Error("invalid token"))
        }
    }
}
@socket()
@middleware(TokenMiddleware)
export class MySocketController extends SocketController {

    private user:any;
    onConnected(){

        this.user = this.socket.request.user
    }

    @action("test")
    public test() {
        return {user:this.user}
    }
}

SocketProvider

holds all the socket controllers. can send messages to all sockets and namespaces

import {ScoketProvider} from "@appolo/scoket";

@define()
export class SomeManager  {

    @inject() socketProvider:ScoketProvider

    notifyAll(){
        this.socketProvider.sendToAll("someEvent",{some:"data"})
    }
}

Methods

  • sendToAll(event: string, data: any) - event message to all clients
  • sendToNamespace(namespace: string, event: string, data: any) - event message to all clients in a namespace
  • get clients(): Map<string, SocketController> - Map of all sockets by id
9.0.3

2 years ago

9.0.2

2 years ago

9.0.1

2 years ago

9.0.0

2 years ago

8.0.1

3 years ago

8.0.0

3 years ago

7.0.1

4 years ago

7.0.0

4 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago