1.0.3 • Published 3 years ago

@jheubuch/ng-ws-template v1.0.3

Weekly downloads
4
License
Apache-2.0
Repository
github
Last release
3 years ago

AsyncAPI Angular WebSocket Template

This template generates a typescript module for WebSocket connections based on rxjs.

Usage

ag asyncapi.json @jheubuch/ng-ws-template -p server=serverName -o outputDir

This will generate your channel services and message models to your specified output directory.

Supported Parameters

NameDescriptionRequiredExample
serverSpecifies the server in the AsyncAPI document which should be used for client generation.Yesserver=production
clientIdSpecifies the client id for the client you are generating the APIs for. (For use with AsyncApiWebSocketMiddleware).NoclientId=FrontendApp

What is being generated

  • The services for communication to the server (can be found at output/services/index.ts)
  • The models used in the Messages (can be found at output/models/{Message}.ts)
  • The API module which provides the services and can be included in the app.module.ts (can be found at output/api.module.ts)

How to use the services

  1. Include ApiModule to your AppModule:
 import { ApiModule } from '{output}/api.module';

 @NgModule({
     ...
     imports: [
         ...,
         ApiModule
     ]
 })
  1. Use the services in your components via dependency injection:
 import { HelloWorldService } from '{output}/services';

 export class AppComponent {
     ...
     constructor(
         private helloWorldService: HelloWorldService
     ) { ... }
 }
  1. Send to and receive from the service:
 // Subscribe to receive data; will be triggered every time something is received on the channel
 this.helloWorldService.subject.subscribe((data => {
     // Do something with your data!
 }));

 // Send data to your channel
 this.helloWorldService.sendToSocket(object);