# Live updates in SolidJS with signals

> Subscribe to Hibira channels from SolidJS with pusher-js and a small primitive that turns an event into a signal, cleaned up with its owner. SolidStart signs private channels on the server.

Source: https://hibira.io/guides/solidjs

Subscribe to Hibira channels from SolidJS with pusher-js and a small primitive that turns an event into a signal, cleaned up with its owner. SolidStart signs private channels on the server.

Package: pusher-js

- createEvent returns a signal that holds the latest event and unsubscribes when the component unmounts.
- A SolidStart API route signs private and presence channels with the pusher npm package.

## 1. Install

```shell
npm install pusher-js pusher
```

## 2. Use it

```tsx
import Pusher from 'pusher-js';
import { createSignal, onCleanup } from 'solid-js';

const pusher = new Pusher(import.meta.env.VITE_HIBIRA_APP_KEY, {
  cluster: 'hkg',
  wsHost: 'ws.hibira.io',
  channelAuthorization: { endpoint: '/api/hibira/auth', transport: 'ajax' },
});

export function createEvent<T>(channelName: string, event: string) {
  const [value, setValue] = createSignal<T>();
  const channel = pusher.subscribe(channelName);
  const handler = (data: T) => setValue(() => data);
  channel.bind(event, handler);
  onCleanup(() => channel.unbind(event, handler));
  return value;
}

// const order = createEvent<{ id: number }>('orders', 'order.shipped');
```

## 3. Sign channel subscriptions

```ts
// src/routes/api/hibira/auth.ts (SolidStart)
import Pusher from 'pusher';

const pusher = new Pusher({
  appId: process.env.HIBIRA_APP_ID!, key: process.env.HIBIRA_APP_KEY!, secret: process.env.HIBIRA_APP_SECRET!,
  host: 'ws.hibira.io', port: '443', useTLS: true,
});

export async function POST({ request }: { request: Request }) {
  const form = await request.formData();
  // check the signed-in user may join this channel first
  return Response.json(pusher.authorizeChannel(String(form.get('socket_id')), String(form.get('channel_name'))));
}
```

## Before you start

- On the Free plan, list every origin your pages load from in the app’s allowed origins (http://localhost:3000 while developing, then your domain). "*" needs a paid plan.
- The app secret is shown only once, when you create the app or rotate its secret. Keep it on your server, never in a browser or game build.

- [npm](https://www.npmjs.com/package/pusher-js)
- [npm](https://www.npmjs.com/package/pusher)
