npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@appolo/socket

v9.0.3

Published

Socket module for [`appolo`](http://www.appolojs.com) build with [socket.io](https://socket.io/)

Downloads

131

Readme

Socket module for appolo build with socket.io

Installation

npm i @appolo/scoket

Options

| key | Description | Type | Default | --- | --- | --- | --- | | id | SocketProvider injection id | string| socketProvider| | auto | true to auto initialize socket listen events | boolean | true | | redis | redis connection for sockets sync | string | null | | socket | socket options | object | {"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