> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ensync.cloud/llms.txt
> Use this file to discover all available pages before exploring further.

# Connect to EnSync

> Initialize the engine and create an authenticated client

## Installation

<Tabs>
  <Tab title="Node.js">
    ```bash theme={null}
    npm install ensync-client-sdk
    ```
  </Tab>

  <Tab title="Python">
    ```bash theme={null}
    pip install ensync-sdk
    ```
  </Tab>
</Tabs>

## Basic Connection

<Tabs>
  <Tab title="Node.js">
    ```javascript theme={null}
    const { EnSyncEngine } = require('ensync-client-sdk');

    // Initialize engine (gRPC with TLS)
    const engine = new EnSyncEngine("grpcs://node.gms.ensync.cloud");
    // Alternative Websocket:
    // const engine = new EnSyncEngine("wss://node.gms.ensync.cloud");

    // Create authenticated client
    const client = await engine.createClient(process.env.APP_KEY, {
      appSecretKey: process.env.APP_SECRET_KEY
    });
    ```
  </Tab>

  <Tab title="Python">
    ```python theme={null}
    from ensync_sdk import EnSyncEngine

    # Initialize engine (gRPC with TLS)
    engine = EnSyncEngine("node.gms.ensync.cloud")

    # Create authenticated client
    client = await engine.create_client(os.environ.get("APP_KEY"), {
      "app_secret_key": os.environ.get("APP_SECRET_KEY")
    })
    ```
  </Tab>
</Tabs>

## Authentication vs Event Targeting

**App Key authenticates your connection**, while **Client ID (appId) targets event delivery**:

* **App Key**: Authenticates who you are when connecting to EnSync
* **Client ID**: Identifies the recipient when publishing events (this is your appId)

When you publish an event, you specify recipient Client IDs. When partners publish to you, they use your Client ID. Everyone publishes and subscribes on the **same bidirectional connection**.

<Info>
  Events are always published to specific Client IDs. Even if multiple partners subscribe to the same event type, they only receive events explicitly addressed to their Client ID.
</Info>

## Connection Options

<Tabs>
  <Tab title="Node.js">
    ```javascript theme={null}
    const engine = new EnSyncEngine("grpcs://node.gms.ensync.cloud", {
      heartbeatInterval: 30000,
      reconnectInterval: 5000,
      maxReconnectAttempts: 5
    });
    ```
  </Tab>

  <Tab title="Python">
    ```python theme={null}
    engine = EnSyncEngine("node.gms.ensync.cloud", {
      "reconnect_interval": 5000,
      "max_reconnect_attempts": 10
    })
    ```
  </Tab>
</Tabs>

## Closing Connections

Always close connections when done to free resources:

<Tabs>
  <Tab title="Node.js">
    ```javascript theme={null}
    // Close just this client
    await client.close();

    // Close client and engine (if you have no other clients)
    await client.close(true);
    ```
  </Tab>

  <Tab title="Python">
    ```python theme={null}
    # Close just this client
    await client.close()

    # Close client and engine (if you have no other clients)
    await client.close(close_engine=True)
    ```
  </Tab>
</Tabs>

## Best Practices

* Store credentials in environment variables, never hardcode them
* Use secure TLS connections in production (`grpcs://` or `wss://`)
* Close connections when done to free resources
