RSocket & AsyncAPI
First, explains the basic method of directly using the /rsocket entry point. The flow of generating route/request/response types and service functions in AsyncAPI is the next optional project integration method.
Basic usage of the RSocket adapter
Install the peer packages only in projects that use RSocket.
Basic usage does not require AsyncAPI generator or WebFlux. Import createRSocketApi from /rsocket and pass the WebSocket endpoint, and only pass JWT as the second argument if authentication is required. Mono or stream is returned as an RxJS Observable.
Optional: AsyncAPI service auto generation project integration.
Almost the same flow as REST.
REST uses swagger.jsonwhile RSocket usesasyncapi-rsocket.jsonRead the route, interaction type, request, and response schema from the documentation to generate TypeScript types and service functions.
1. First: Check what rsocketCommonService is with the full code.
rsocketCommonService.ts is also not a file automatically generated by the library. It wraps createRSocketApi directly in the project to centralize connection/JWT reuse policies, allowing the generated RSocket service to only import callRSocketMono/callRSocketStream. Below is the full code of that file. It reads Authorization from the headerStore of the REST common service, calls the token API once to prepare the JWT if it is missing, and closes the existing RSocket connection and creates a new one if the JWT changes.
rsocketCommonService.ts
tsAfter copying, modify the headerStore import path, AsyncApiTypes location, token API path, NEXT_PUBLIC_RSOCKET_URL, and the actual RSocket endpoint to fit your project.
2. Role of the rsocketCommonService code structure.
getRSocketUrl
On the server, environment variables are used, and in the browser, it creates ws or wss URLs based on the current protocol and host.
fetchAuthorization
When there is no JWT in headerStore, it calls the token API including the session cookie and reads values from the token, accessToken, or jwt fields.
getRsoketApi
Even with multiple requests coming in simultaneously, it shares the token retrieval Promise, reuses existing connections for the same JWT, and creates a new connection if the JWT changes.
resetRsoketApi
On logout or when removing authentication information, it closes the current RSocket connection and resets the stored JWT and client.
callRSocketMono / callRSocketStream
Checks request and response with the generated route type, and merges the connection preparation Promise into the RxJS Observable flow to execute mono or stream.
3. AsyncAPI service generation.
generateAsyncApiRSocket.cjs
js4. Calling the generated function.
The following is a typical requestStream example that receives the progress of multiple task processes. The route string and event type are included in the generated service.
BatchJobPage.tsx
tsxChoose between mono and stream.
If the AsyncAPI interaction is requestResponsethe created function usescallRSocketMono. requestStreamIf it is callRSocketStreamit uses. nextOn the screen, both are Observable, but the stream can be called multiple times.