@uon/websocket
TypeScript icon, indicating that this package has built-in type declarations

0.9.0 • Public • Published

UON WebSocket

Websocket server and client implementation for node.

Installation

    npm i @uon/websocket

Usage

To start using @uon/websocket, include it in your app module's imports alongside HttpModule.

import { HttpModule } from '@uon/http';
import { WebSocketModule } from '@uon/websocket';

@Module({
    imports: [
        HttpModule.WithConfig({
            plainPort: 8080
        }),
        WebSocketModule
    ]
})
export class MyAppModule {}

Routing upgrade requests

import { RouterOutlet } from '@uon/router';
import { HttpRoute, HttpContext } from '@uon/http';

@RouterOutlet()
export class MyAppOutlet {

    constructor(private context: HttpContext) {}

    @HttpRoute({
        method: 'UPGRADE', // <-- Special method for protocol upgrade requests
        path: '/my-ws-channel'
    })
    upgradeToWs() {

        // do the protocol upgrade
        let ws = await this.context.upgrade(WebSocket, {
            'X-My-Extra-Header': '1234' // <-- extra headers to send with upgrade response
        });

        // send and receive
        await ws.send('Hello');

        ws.on('message', (msg: Buffer) => {
            ws.send('Well received.');
        });


    }

}

Read more on routing here.

Readme

Keywords

Package Sidebar

Install

npm i @uon/websocket

Weekly Downloads

2

Version

0.9.0

License

MIT

Unpacked Size

64.9 kB

Total Files

24

Last publish

Collaborators

  • uon-io