Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new decorator @WebSocketRoute() and @WebSocketDriver() #715

Closed
samchon opened this issue Dec 7, 2023 · 0 comments
Closed

Add new decorator @WebSocketRoute() and @WebSocketDriver() #715

samchon opened this issue Dec 7, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@samchon
Copy link
Owner

samchon commented Dec 7, 2023

Will add new decorators @WebSocketRoute() and @WebSocketDriver(), which wrap TGrid features.

With those decorators, NestJS users can develop websocket server much type safely.

Also, if such websocket decorators being used, automatically generated SDK will also contain the websocket features with tgrid library importing. Of course, in the SDK generation case, the generic arguments of WebSocketAcceptor<ChatServerProvider, ChatClientProvider> would be important.

NestJS Controller

import core from "@nestia/core";
import { Controller } from "@nestjs/common";

@Controller()
export class AppController {
  @core.TypedGet("health")
  public health(): string {
    return "Hello World";
  }

  @core.WebSocketRoute("chat/:chatRoomId", {
    encrypt: true,
  })
  public async chat(
    @core.TypedParam("chatRoomId") id: string,
    @MemberAuth() member: IMember,
    @core.WebSocketAcceptor() 
    acceptor: core.WebSocketAcceptor<ChatServerProvider, ChatClientProvider>,
  ): Promise<void> {
    await acceptor.provider({
      ...
    } as ChatServerProvider);
    await acceptor.join();
  }
}

Providers

export interface ChatServerProvider {
  talk(content: string): void;
  whisper(to: string, content: string): void;
}
export interface ChatClientProvider {
  talk(from: string, content: string): void;
  whisper(from: string, content: string): void;
}

Client Use Case

import sdk from "sdk-library-generated-by-nestia";

const provider: ChatClientProvider = {
  talk: (from, content) => {
    console.log(`${from}: ${content}`);
  },
  whisper: (from, content) => {
    console.log(`> ${from}: ${content}`);
  },
};

const main = async (): Promise<void> => {
  const connection = {
    host: "http://localhost",
    encryption: {
      key: "abcdefghijklmnop",
      iv: "abcdefgh",
    },
  };
  const websocket: WebConnector<ChatClientProvider, ChatServerProvider> = 
    await sdk.chat(
      connection,
      "some-chat-room-id", 
      ChatClientProvider
    );
  const serverProvider: Driver<ChatServerProvider> = connection.getDriver();
  await serverProvider.talk("Yaho, Hello friends");
  await serverProvider.whisper("someone", "Do you know me?");
  await serverProvider.close();
};
@samchon samchon added the enhancement New feature or request label Dec 7, 2023
@samchon samchon self-assigned this Dec 7, 2023
samchon added a commit that referenced this issue Apr 28, 2024
Close #715: `@WebSocketRoute()` decorators.
samchon added a commit to samchon/tgrid that referenced this issue Apr 28, 2024
To make clear type of acceptors and connectors, added new generic argument `Remote`.

This would be especially useful for samchon/nestia#715.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
No open projects
Status: Done
Development

No branches or pull requests

1 participant