跳過到頁腳內容
PYTHON 幫助

deepstream io(開發人員的工作原理)

即時伺服器旨在即時回應數據,使其能夠即時回應每個使用者互動或系統事件。 與引入延遲的傳統請求回應伺服器不同,即時[伺服器](https://en.wikipedia.org/wiki/Server_(computing)使用技術和協定來確保持續的資訊交換和即時更新。 這是因為即時伺服器對於許多需要即時通訊的應用至關重要:訊息系統、線上遊戲、金融交易平台和協作工具。 在本文中,我們將學習如何使用開源即時伺服器 deepstream 和IronPDF產生 PDF 檔案。

deepstream.io是可擴展的即時伺服器,用於資料同步和多對多訊息傳遞。 它可以輕鬆處理數據,並以極低的延遲保持多個客戶端同步,支援二進位數據傳輸。 deepstream.io 旨在置於其他負載平衡器和負載平衡器之後,並提供了一種高效的方式來同步資料並保持資源檔案的最新狀態,這使得它非常適合即時更新資料的應用程式以及尋求可擴展伺服器解決方案的應用程式。

deepstream.io(開發者使用方法):圖 1 - deepstream.io

開發者可以透過 deepstream.io 輕鬆實現即時更新、協作應用程式和即時互動體驗,而無需從頭開始。 它採用高負載和高效擴展架構,是高並發應用程式的首選軟體。 deepstream.io 非常靈活,可能會以多種不同的方式成為您技術堆疊的完美補充。 它提供了一套完整的解決方案,使用戶能夠創建即時、響應式和互動式的 Web 和行動應用程式。

若要在控制台中建立一個新的Node.js目錄,請輸入以下指令:

mkdir deepstream-project
cd deepstream-project
npm init -y
mkdir deepstream-project
cd deepstream-project
npm init -y
SHELL

安裝 deepstream.io 套件

首先,你需要安裝 deepstream.io。 您可以使用 NPM 進行安裝,也可以從官方網站下載二進位。

npm install @deepstream/server
npm install @deepstream/server
SHELL

deepstream.io 的基本配置

const { Deepstream } = require('@deepstream/server');

// Create a new Deepstream server instance
const server = new Deepstream({});

// Start the server to listen for client connections
server.start();
const { Deepstream } = require('@deepstream/server');

// Create a new Deepstream server instance
const server = new Deepstream({});

// Start the server to listen for client connections
server.start();
JAVASCRIPT

上面的程式碼片段示範如何使用Node.js中的 @deepstream/server 套件來設定和啟動 deepstream.io 伺服器。 它首先從套件中導入 Deepstream 類,然後建立一個新實例。 透過呼叫 server.start(),伺服器啟動並準備好接受傳入的連接,以處理即時資料綁定、訊息傳遞或存在後端服務。

將 Deepstream 與客戶端連接

const { DeepstreamClient } = require('@deepstream/client');

// Connect to a local deepstream server
const client = new DeepstreamClient('127.0.0.1:6020');

// Log in to the server without credentials (suitable for servers without authentication)
client.login(null);
const { DeepstreamClient } = require('@deepstream/client');

// Connect to a local deepstream server
const client = new DeepstreamClient('127.0.0.1:6020');

// Log in to the server without credentials (suitable for servers without authentication)
client.login(null);
JAVASCRIPT

上面的程式碼示範如何使用 @deepstream/client 函式庫連接到 deepstream。 此腳本導入 DeepstreamClient 類,建立一個實例,並將其連接到本地運行在 IP 位址 127.0.0.1 連接埠 6020 上的 deepstream 伺服器。它無需憑證即可登錄,如果伺服器不使用身份驗證或用於測試用例,則這已足夠。 此設定初始化一個能夠進行即時資料同步和通訊的用戶端。

與伺服器節點建立連線後,伺服器控制台中將顯示類似如下的訊息。

deepstream io(開發者使用方法):圖 2 - 控制台訊息

使用 deepstream.io 的監聽器

下面是一個可用於建立監聽器的範例程式碼,監聽器是 deepstream 的核心概念之一。

const { DeepstreamClient } = require("@deepstream/client");

// Connect to the Deepstream server
const client = new DeepstreamClient("127.0.0.1:6020");

// Log in to the server
client.login(null, (success, clientData) => {
  if (success) {
    const event = client.event;
    // Publish a custom event
    event.publish("custom-event", { message: "Hello, Deepstream!" });
  }
});
const { DeepstreamClient } = require("@deepstream/client");

// Connect to the Deepstream server
const client = new DeepstreamClient("127.0.0.1:6020");

// Log in to the server
client.login(null, (success, clientData) => {
  if (success) {
    const event = client.event;
    // Publish a custom event
    event.publish("custom-event", { message: "Hello, Deepstream!" });
  }
});
JAVASCRIPT

在上面的程式碼中,用戶端使用 @deepstream/client 庫登入託管在 127.0.0.1:6020 上的 deepstream 伺服器。 驗證成功後,它會發布一個名為"custom-event"的自訂事件,有效負載為{ message: "Hello, Deepstream!" }

IronPDF簡介

使用功能強大的Node.js套件IronPDF來建立、編輯、轉換和修改 PDF 文件。 它是大多數基於程式設計的 PDF 處理流程以及後端流程(例如修改現有 PDF 和將 HTML 轉換為 PDF)中使用的工具。 在需要動態建立和處理 PDF 的應用中, IronPDF就顯得非常有用。 它提供了一種用戶友好且靈活的方式來產生高品質的PDF文件。

deepstream io(開發者使用方法):圖 3 - IronPDF

安裝IronPDF軟體包

使用 npm 下載並安裝啟用Node.js IronPDF功能的軟體包。

npm install @ironsoftware/ironpdf
npm install @ironsoftware/ironpdf
SHELL

PDF產生功能

建立一個使用IronPDF產生 PDF 的函數:

const IronPdf = require('@ironsoftware/ironpdf');
const { PdfDocument } = IronPdf;

// Set IronPDF configuration, replacing 'YOUR_LICENSE_KEY' with your actual license key
const config = IronPdf.IronPdfGlobalConfig;
config.setConfig({ licenseKey: 'YOUR_LICENSE_KEY' });

async function generatePDF(title, content) {
  try {
    // Generate PDF from HTML content
    const pdf = await PdfDocument.fromHtml(`<html><body><h1>${title}</h1><p>${content}</p></body></html>`);
    return await pdf.saveAsBuffer();
  } catch (error) {
    console.error('Error generating PDF:', error);
    throw error;
  }
}

module.exports = generatePDF;
const IronPdf = require('@ironsoftware/ironpdf');
const { PdfDocument } = IronPdf;

// Set IronPDF configuration, replacing 'YOUR_LICENSE_KEY' with your actual license key
const config = IronPdf.IronPdfGlobalConfig;
config.setConfig({ licenseKey: 'YOUR_LICENSE_KEY' });

async function generatePDF(title, content) {
  try {
    // Generate PDF from HTML content
    const pdf = await PdfDocument.fromHtml(`<html><body><h1>${title}</h1><p>${content}</p></body></html>`);
    return await pdf.saveAsBuffer();
  } catch (error) {
    console.error('Error generating PDF:', error);
    throw error;
  }
}

module.exports = generatePDF;
JAVASCRIPT

設定 Deepstream 客戶端

編寫一個JavaScript腳本,即時監聽資料並根據這些資料產生 PDF 檔案:

const { DeepstreamClient } = require('@deepstream/client');
const generatePDF = require('./generatePdf'); // Path to your PDF generation function

// Connect to the Deepstream server
const client = new DeepstreamClient('127.0.0.1:6020');

// Log in to the server
client.login(null, async (success) => {
  if (success) {
    console.log('Deepstream connected successfully');
    // Listen for a custom event to trigger PDF generation
    const event = client.event;
    event.subscribe('generate-pdf', async (data) => {
      const { title, content } = data;
      if (!title || !content) {
        console.error('Missing title or content for PDF generation');
        return;
      }
      try {
        // Generate the PDF
        const pdfBuffer = await generatePDF(title, content);
        // Handle the PDF buffer (e.g., save to file, send over network)
        console.log('PDF generated successfully');
        // Example: Save PDF to a file (optional)
        const fs = require('fs');
        fs.writeFileSync('generated.pdf', pdfBuffer);
      } catch (error) {
        console.error('Error generating PDF:', error);
      }
    });
  } else {
    console.error('Failed to connect to Deepstream');
  }
});
const { DeepstreamClient } = require('@deepstream/client');
const generatePDF = require('./generatePdf'); // Path to your PDF generation function

// Connect to the Deepstream server
const client = new DeepstreamClient('127.0.0.1:6020');

// Log in to the server
client.login(null, async (success) => {
  if (success) {
    console.log('Deepstream connected successfully');
    // Listen for a custom event to trigger PDF generation
    const event = client.event;
    event.subscribe('generate-pdf', async (data) => {
      const { title, content } = data;
      if (!title || !content) {
        console.error('Missing title or content for PDF generation');
        return;
      }
      try {
        // Generate the PDF
        const pdfBuffer = await generatePDF(title, content);
        // Handle the PDF buffer (e.g., save to file, send over network)
        console.log('PDF generated successfully');
        // Example: Save PDF to a file (optional)
        const fs = require('fs');
        fs.writeFileSync('generated.pdf', pdfBuffer);
      } catch (error) {
        console.error('Error generating PDF:', error);
      }
    });
  } else {
    console.error('Failed to connect to Deepstream');
  }
});
JAVASCRIPT

發布事件以觸發 PDF 生成

您可以發布事件,以觸發從另一個JavaScript檔案或應用程式的某個部分產生 PDF 檔案:

const { DeepstreamClient } = require('@deepstream/client');

// Connect to the Deepstream server
const client = new DeepstreamClient('127.0.0.1:6020');

// Log in to the server
client.login(null, () => {
  const event = client.event;
  // Publish a custom event with title and content
  event.publish('generate-pdf', {
    title: 'Sample PDF Title',
    content: 'This is the content of the PDF document.'
  });
});
const { DeepstreamClient } = require('@deepstream/client');

// Connect to the Deepstream server
const client = new DeepstreamClient('127.0.0.1:6020');

// Log in to the server
client.login(null, () => {
  const event = client.event;
  // Publish a custom event with title and content
  event.publish('generate-pdf', {
    title: 'Sample PDF Title',
    content: 'This is the content of the PDF document.'
  });
});
JAVASCRIPT

Deepstream.io 的實作方式是監聽即時事件,這些事件會觸發PDF 產生generatePDF 函數使用IronPDF根據來自 Deepstream.io 事件的資料建立 PDF 文件。 DeepstreamClient 訂閱這些事件,每當發布相關事件時,它都會呼叫 PDF 生成函數。 這種整合可以根據事件發生、請求或資料變更即時動態產生 PDF。

deepstream io(開發者使用方法):圖 4 - PDF 輸出

授權

要使程式碼能夠編譯並運行而不帶浮水印,需要許可證密鑰。 想要獲得試用許可證的開發者可以在這裡註冊。 取得此券無需出示信用卡。 您只需輸入您的電子郵件地址即可註冊免費試用。

結論

deepstream.io 和IronPDF的結合實現了最強大的即時資料處理和動態文件生成解決方案之一。 deepstream.io 會即時同步變更並記錄所有事件,因此可以立即對資料的任何變更做出反應。 IronPDF提供了一個強大的機制,可以快速建立Professional文件。 透過此集成,您的應用程式不僅可以在即時資料變更時自動建立和處理 PDF 文檔,而且在使用者與您的應用程式互動時也可以自動建立和處理 PDF 文件。

無論是產生報告、發票或其他任何文檔類型,deepstream.io 與IronPDF整合後,可簡化工作流程、快速建立文檔,並利用即時資訊保持應用程式的精簡和最新狀態。 這種組合最適合需要即時文件產生和管理支援的敏捷、資料驅動和響應式應用程式。

Iron Software提供的函式庫使我們能夠快速輕鬆地為各種作業系統、瀏覽器和平台(包括 Windows、Android、MAC、Linux 等)建立程式。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我