Example Node.js Server Setup
Relevant source files
The following files were used as context for generating this wiki page:
The Example Node.js Server Setup provides a reference implementation for a backend environment that integrates with the Centrifugo real-time server. Its primary purpose is to demonstrate how to manage a Centrifugo instance and interact with it using gRPC for administrative tasks, such as broadcasting messages to Tauri-based clients.
This setup serves as the server-side counterpart to the tauri-plugin-centrifugo plugin, enabling developers to test real-time features like channel subscriptions, message publications, and RPC calls in a controlled environment.
Sources: examples/server/README.md:1-5, README.md:10-15
Server Architecture and Components
The server infrastructure consists of two main components: the Centrifugo engine itself and a Node.js-based "info-sender" script. The Centrifugo engine handles WebSocket connections from frontend clients, while the Node.js script acts as an administrative backend using the gRPC API to push data into the system.
The diagram shows the communication flow where the Node.js script sends administrative updates to Centrifugo via gRPC, which then pushes those updates to the Tauri application via WebSockets.
Sources: examples/server/README.md:14-23, examples/tauri-app/src/App.svelte:361-370
Network Configuration
The environment relies on specific port mappings for different communication protocols:
| Port | Protocol | Description |
|---|---|---|
| 8001 | HTTP/WebSocket | Main server port for client connections. |
| 10001 | gRPC | Administrative API port for backend communication. |
| 1420 | HTTP | Allowed origin for the Tauri development server. |
Sources: examples/server/README.md:14-18
Backend Implementation Details
The backend is built using TypeScript and utilizes @grpc/grpc-js to interface with Centrifugo's API. The package.json defines the necessary scripts to initialize, build, and run both the server and the messaging script.
Dependency Management
The server environment requires Node.js (version 18 or higher) and uses several key libraries for gRPC and TypeScript support:
- @grpc/grpc-js: Core library for gRPC communication.
- @grpc/proto-loader: For loading the
api.protodefinitions dynamically. - ts-node: For running TypeScript files during development.
Sources: examples/server/package.json:26-34, examples/server/package.json:37-39
gRPC API Definition
The communication between the Node.js script and Centrifugo is governed by a Protobuf definition. This API supports a wide range of administrative actions.
This diagram represents a subset of the methods and data structures available in the api.proto for backend-to-server communication.
Sources: examples/server/proto/api.proto:7-20, examples/server/proto/api.proto:136-143
Deployment and Execution
The project provides a streamlined setup process using npm or pnpm to manage the lifecycle of the server components.
Lifecycle Commands
The following commands are used to manage the server setup:
npm run setup: Installs dependencies and builds the TypeScript source.npm run centrifugo:dev: Launches the Centrifugo server in debug mode usingcentrifugo.json.npm run dev: Executes the message sending script (info-sender.ts) to begin broadcasting data.
Sources: examples/server/README.md:7-12, examples/server/package.json:7-13
Centrifugo Configuration
The server behavior is defined in centrifugo.json, which configures security settings, allowed origins (including the Tauri port 1420), and API keys necessary for the Node.js script to authenticate with the gRPC service.
Sources: examples/server/README.md:21, examples/server/README.md:18
Message Flow Example
When the Node.js server setup is active, messages flow from the backend script through Centrifugo to the Tauri frontend. The frontend utilizes the tauri-plugin-centrifugo-api to receive and decode these messages.
This sequence illustrates how a message originated in the Node.js backend is propagated to the end-user application.
Sources: examples/tauri-app/src/App.svelte:115-135, examples/server/proto/api.proto:7-9
Conclusion
The Example Node.js Server Setup is a critical utility for developing with the tauri-plugin-centrifugo. By providing a gRPC-enabled backend and a pre-configured Centrifugo instance, it allows for end-to-end testing of real-time data streams, ensuring that the Tauri application correctly handles incoming publications and administrative commands.