# @holochain-open-dev/cell-client

> Temporary wrapper around @holochain/client and @holo-host/web-sdk

Latest version **0.9.1** (published 2022-12-08) · ISC license · 0 weekly downloads

## Install

```sh
npm install @holochain-open-dev/cell-client
pnpm add @holochain-open-dev/cell-client
yarn add @holochain-open-dev/cell-client
bun add @holochain-open-dev/cell-client
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.9.1 |
| Published | 2022-12-08 |
| First published | 2021-07-15 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 240 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | matthme, zippy314, guillem.cordoba |

## Links

- npm: https://www.npmjs.com/package/@holochain-open-dev/cell-client
- Repository: https://github.com/holochain-open-dev/cell-client
- Homepage: https://github.com/holochain-open-dev/cell-client#readme
- Issues: https://github.com/holochain-open-dev/cell-client/issues
- npm.io page: https://npm.io/package/@holochain-open-dev/cell-client

## Dependencies (4)

- [@types/ws](https://npm.io/package/@types/ws.md) ^8.5.3
- [lodash-es](https://npm.io/package/lodash-es.md) ^4.17.21
- [@holochain/client](https://npm.io/package/@holochain/client.md) ^0.10.2
- [@holo-host/web-sdk](https://npm.io/package/@holo-host/web-sdk.md) 0.6.7-prerelease

## Recent versions

- 0.9.1 (latest) — 2022-12-08
- 0.9.0 — 2022-12-07
- 0.8.0 — 2022-10-07
- 0.7.3 — 2022-09-01
- 0.7.2 — 2022-08-20
- 0.7.1 — 2022-08-09
- 0.7.0 — 2022-08-08
- 0.6.1 — 2022-07-20
- 0.6.0 — 2022-07-06
- 0.5.3 — 2022-06-21
- 0.5.2 — 2022-06-21
- 0.5.1 — 2022-06-21
- 0.5.0 — 2022-06-21
- 0.4.2 — 2022-06-21
- 0.4.1 — 2022-06-15
- … 23 more at https://npm.io/package/@holochain-open-dev/cell-client/versions

## README

# Cell Client

Temporary wrapper around `@holochain/client` and `@holo-host/web-sdk`.

This is useful to build agnostic UIs, that can potentially connect both with Holo and with Holochain infrastructure transparently.

Note that this will only be useful until there is a unified API for both cases.

## Installing

```bash
npm i @holochain-open-dev/cell-client
```

## Setup

### Connecting to Holochain

```ts
import { HolochainClient } from "@holochain-open-dev/cell-client";
import { RoleName, AppWebsocket } from "@holochain/client";

async function setupClient() {
  const appWebsocket = await AppWebsocket.connect(
    "ws://localhost:8888"
  );

  const client = new HolochainClient(appWebsocket);

  return client;
}
```

### Connecting to Chaperone (Holo)

Follow the main instructions for the @holo-host/web-sdk [here](https://github.com/Holo-Host/web-sdk). Then, create a `HoloClient`:

```ts
import { HoloClient } from "@holochain-open-dev/cell-client";
import WebSdk from '@holo-host/web-sdk'

async function setupClient() {
  const client = await WebSdk.connect({
    chaperoneUrl: 'http://localhost:24274' // Connect to holo-dev-server
    authFormCustomization: {
      logoUrl: "my-logo.png",
      appName: "My App",
      requireRegistrationCode: false
    }
  });

  // We just started up, so we're still connecting. Let's wait for isAvailable == true
  const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
  while (!client.agent.isAvailable) {
    await sleep(50)
    // In a real UI, we would register an event handler for `client.on('agent-state')`
    // and store the agent state in a reactive UI state so that our components can just branch on isAvailable.
  }

  // WebSdk defaults to an anonymous connection where you can't write to the source chain. Sign in so we can commit something
  await client.signIn()
  
  // The credentials overlay is now visible to the user. Wait for them to sign in
  while (client.agent.isAnonymous || !client.agent.isAvailable) {
    await sleep(50)
    // Again, this while/sleep pattern is for demo only. See comment above about doing this using an event handler
  }

  const appInfo = await client.appInfo();

  return new HoloClient(client, appInfo);
}
```

## Usage

There are two layers that you can use:

### HolochainClient / HoloClient (both implement the AgnosticClient interface)

Use this layer if you need to be switching which cell you are making the call to. This is specially needed in apps that use the pattern of cloning cells.

```ts
const client: HolochainClient = await setupClient();

const appInfo = await client.appWebsocket.appInfo({
  installed_app_id: 'test-app'
});

// Find the cell you want to make the call to
const cellId = appInfo.cell_data[0].cell_id;

const response = await client.callZome(cellId, "my-zome", "my_fn_name", {
  this: "is a sample payload",
});

const { unsubscribe } = client.addSignalHandler((signal) =>
  console.log("Received signal from any of the cells", signal)
);

...

// You can unsubscribe from listening to the signal whenever needed
unsubscribe();
```

Here, use `AgnosticClient` instead of `HolochainClient` or `HoloClient` if you want your calls to be Holo/Holochain agnostic.

### CellClient

Use this layer if you only have one cell, or predefined set of cells.

```ts
const client: HolochainClient = await setupClient();

const appInfo = await client.appWebsocket.appInfo({
  installed_app_id: 'test-app'
});

// Find the cell you want to make the call to
const cell = appInfo.cell_data[0];

const cellClient = new CellClient(client, cell);

// Now the calls and signals will only interact with the desired cell
const response = await cellClient.callZome("my-zome", "my_fn_name", {
  this: "is a sample payload",
});

const { unsubscribe } = client.addSignalHandler((signal) =>
  console.log(`Received signal for cell`, cell, signal)
);

...

// You can unsubscribe from listening to the signal whenever needed
unsubscribe();
```

---
_Source: https://npm.io/package/@holochain-open-dev/cell-client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
