# Function: observable()

> 

> **observable**\<`TValue`, `TError`\>(`subscribe`): [`Observable`](https://trpc.io/docs/typedoc/server/observable/interfaces/Observable.md)\<`TValue`, `TError`\>

Defined in: [packages/server/src/observable/observable.ts:38](https://github.com/trpc/trpc/blob/0813cebf5c04e692e93f9cbc01b7f8678faa53b3/packages/server/src/observable/observable.ts#L38)

Creates an observable. `subscribe` runs for each subscriber, and can
return a function that cleans up when it unsubscribes.

## Type Parameters

| Type Parameter | Default type |
| ------ | ------ |
| `TValue` | - |
| `TError` | `unknown` |

## Parameters

| Parameter | Type |
| ------ | ------ |
| `subscribe` | (`observer`) => [`TeardownLogic`](https://trpc.io/docs/typedoc/server/observable/type-aliases/TeardownLogic.md) |

## Returns

[`Observable`](https://trpc.io/docs/typedoc/server/observable/interfaces/Observable.md)\<`TValue`, `TError`\>

## Example

```ts
const ticks = observable<number>((observer) => {
  const timer = setInterval(() => observer.next(Date.now()), 1000);
  return () => clearInterval(timer);
});
```

## See

https://trpc.io/docs/client/links#creating-a-custom-link
