How tjchaplin/signalrjs works
Main Components
• Client Layer: Represented by the example HTML files in the “docs/examples” directories. These clients use jQuery and the bower-managed SignalR JavaScript client to communicate with the server.
• Server Layer: This is built on Node.js using Express (see index.js and the sample code in the README). The SignalRJS instance is added as middleware via signalR.createListener().
• SignalRJS Core Library: The main functionality is implemented here. Break this layer into subcomponents:
– Hubs: Found under lib/hubs (includes hubFactory.js, hubScriptFactory.js, templateHub.js, and hubs.js). Hubs encapsulate real-time messaging and server methods which clients call.
– Connections: Implemented in lib/connections (connectionManager.js, userTokens.js). This module is responsible for managing active client connections and handling their state.
– Transports: Under lib/transports, features like longPollingTransport.js and sseTransport.js offer different transport mechanisms. They define how real-time communication is achieved based on client capabilities.
– Common Utilities: In lib/common, utilities such as eventConstants.js, messageFactory.js, objectExtensions.js, and signalrRequestParser.js support the core operations.
– The SignalRJS Bootstrap Module: Likely contained in index.js and/or signalRJS.js (within lib/signalRJS.js) which wired everything up.
• Testing: Represented by items in the tests/ directory which show how the hubs and signalRJS functionalities are verified.
Relationships and Data Flows
• Within the server, emphasize how Express integrates the SignalRJS listener middleware. Show that the incoming requests are processed by the SignalRJS core.
• Detail the internal communications: show how the signalR instance negotiates connection details, then routes them either to the appropriate hub (using the hub module) or via the configured transport (long polling or SSE). Also note that message delivery flows “inside” the hubs where server methods (like send) are invoked.
• Within the hubs, illustrate the bi-directional communication – from server methods being called by client requests, then established connections broadcasting or sending messages back to all or selected clients.
• If applicable, show the authentication layer: For example, the passportChat example in docs shows integration with Passport, so add an “Authentication” or “Passport” component that affects the connection handling and user identity setup.
Important Architectural Patterns and Technologies
• Middleware Pattern: Express middleware is used for routing messages.
• Modular Design: The library is divided into clear functional modules (hubs, connections, transports, and common utilities), which improves separation of concerns.
• Client-Server Communication Pattern: Real-time messaging via Ajax/HTTP long polling or event streams (SSE), enabling dynamic interaction between client and server.
• Dependency Injection/Factory Patterns: In hubs, factories are used for creating hub instances and generating hub scripts (hubFactory and hubScriptFactory).
• Asynchronous/Non-blocking Architecture: Leveraging Node.js’s event-driven model.
Technical Stack Notables
• Technologies: Node.js, Express, jQuery, and Passport (for authentication in one of the examples).
• Transports: Long Polling (for older browsers like IE > 10) and Server Sent Events (for modern browsers like Chrome and Firefox).