A WebSocket API can be very useful for real-time connection applications and chat apps. More recently, with the giant flow of apps using LLM tools, it has become unviable to use classical HTTP APIs to run those LLM services. With that said, this code serves as a basis for developers to create their own intelligent service provider.
npm installnpm startrun from the root directory
const ws = new WebSocket("ws://localhost:8080");when in production, use the servers address
I created a simple client that connects to the server and sends different types of messages. The client was built using Next js and has a simple UI that allows you to send messages to the server. Check the repo link below.
sequenceDiagram
participant Client
participant HTTPServer
participant WebSocketServer
participant RequestHandlerManager
participant MessageHandlerFactory
participant MessageHandler
Client->>HTTPServer: GET /get-token?username=user1
HTTPServer-->>Client: 200 OK { token: "JWT" }
Client->>WebSocketServer: WebSocket Connection with JWT
WebSocketServer->>WebSocketServer: Verify JWT
WebSocketServer-->>Client: Connection Established
WebSocketServer->>RequestHandlerManager: Instantiate RequestHandlerManager(ws)
RequestHandlerManager->>WebSocketServer: Add Event Listeners
Client->>RequestHandlerManager: Send Message
RequestHandlerManager->>RequestHandlerManager: handleMessage(event.data)
RequestHandlerManager->>MessageHandlerFactory: createHandler(jsonReq.type, ws)
MessageHandlerFactory->>MessageHandler: Instantiate Appropriate Handler
MessageHandlerFactory-->>RequestHandlerManager: Return Handler
RequestHandlerManager->>MessageHandler: handler.handle(jsonReq)
MessageHandler->>Client: Send Response
You can modify the RequestHandler class to handle the messages as you wish. Or even turn that into a factory to instantiate different handlers for different types of clients. The idea is that the handlers are responsible for the business logic of the application. They can process the messages, send messages to other clients, or even trigger events in other services like LLMs.
- Implement a more robust JWT verification
- Implement a more robust message handling system (maybe switching from WebSocket to Socket.io can make it easier)
- Implement an SDK that holds defined types of messages that could be shared between the client and the server
- This code is a simple example of a WebSocket server. It is not production-ready. For more robuts systems, consider using routing libraries that can help you create different sockets for different types of messages.
- This code is not a full implementation of a JWT verification system. It is just a simple example of how you can use JWT to authenticate your clients. The client and the server need to share a key to generate and verify the JWT.
- This code is not a full implementation of a message handling system. It is just a simple example of how you can use a factory to instantiate different handlers for different types of messages. You can create a more robust system that can handle different types of messages and different types of clients.
