フッターコンテンツにスキップ
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は柔軟で、多様な方法でスタックに最適な追加になる可能性があります。 リアルタイムで応答し、インタラクティブなウェブおよびモバイルアプリをユーザーが作成できる完全なソリューションを提供します。

新しい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 - コンソールメッセージ

Listenerを使用したdeepstream.io

以下に、deepstreamのコアコンセプトの1つであるリスナーを作成するために使用できるサンプルコードを示します。

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の紹介

IronPDFを使用して、Node.jsのインクレディブルストロングパッケージでPDFドキュメントを作成、編集、変換、編集できます。 PDFに基づくほとんどのプログラミングプロセス、および既存のPDFの変更やHTMLをPDFに変換するバックエンドプロセスで使用されるツールです。 PDFの動的な作成と処理が必要なアプリケーションでは、IronPDFが非常に役立ちます。 クオリティの高いPDFドキュメントを生成するためのユーザーフレンドリーで柔軟な方法を提供します。

deepstream io(開発者向けの動作方法):図3 - IronPDF

IronPDFパッケージのインストール

Node.js IronPDF機能を有効にするパッケージをnpmでダウンロードしてインストールします。

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クライアントの設定

リアルタイムでデータを監視し、そのデータに基づいてPDFを生成するJavaScriptスクリプトを書きます:

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は、プロフェッショナルドキュメントをフライで作成するための強力なメカニズムを提供します。 この統合により、ライブデータの変化だけでなく、ユーザーがアプリケーションとやりとりするたびに自動的にPDF文書を作成し処理することができます。

これがレポート生成、請求書、または他の任意の文書であっても、deepstream.ioとIronPDFの統合により、ワークフローの簡素化、応答性の高いドキュメント作成が可能になり、アプリケーションを効率化し、リアルタイム情報で最新の状態に保ちます。 このペアリングは、リアルタイムのドキュメント生成と管理サポートを要求するアジャイルでデータ駆動型のレスポンシブアプリケーションに最適です。

<Iron Softwareが提供するライブラリにより、Windows、Android、MAC、Linuxなど、さまざまなオペレーティングシステム、ブラウザ、プラットフォーム向けのプログラムを素早く簡単に作成できます。

Curtis Chau
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。