# Signalz

## Documentation

Hello this is the documentation

## Installation

Simply just run:

```bash
$ npm i signalz
```

## Importing

```javascript
// Using Node.js `require()`
const Signalz = require("signalz");

// Using ES6 imports
import Signalz from "signalz";
```

## Firebase

Heres how to setup Signalz with the firebase method

### Getting Started

First import firebase-admin and initialize firebase, this is explained further [here](https://firebase.google.com/docs/admin/setup/#initialize-sdk)

```javascript
const firebase = require("firebase-admin");

const serviceAccount = require("./serviceAccountKey.json");
if (firebase.apps.length === 0) {
  firebase.initializeApp({
    credential: firebase.credential.cert(serviceAccount),
    databaseURL: "YOUR_DB_URL",
  });
}
```

Then initialize Signals itself:

```javascript
const client = new Signalz.Client("CLIENT_ID", firebase.database());
```

Client ID is explained [here](https://signalz.js.org/broken-reference)

### Sending a Signal

```javascript
await client.sendSignal("SIGNAL_ID", "TARGET_CLIENT_ID", data);
// Data can be a number, string, boolean, or object.
```

Signal ID and Target Client ID are exmplaned [here](https://signalz.js.org/broken-reference)

### Receiving a Signal

```javascript
client.on("signal", (signal) => {
  // do stuff
});
```

### Replying to Signal

After doing the code when receiving a signal, its a good practice to reply to that signal with a status of "accept" or "error" to make the other project know the status of the signal, optionally, you can add a message to the reply

```javascript
client.on("signal", (signal) => {
  // do stuff
  signal.reply("accept", "good!");
  // OR
  signal.reply("error", "invalid yada yada");

  // Message can be number, string, boolean, or object.
});
```

### Awaiting a Reply

```javascript
const sentSignal = await client.sendSignal(
  "SIGNAL_ID",
  "TARGET_CLIENT_ID",
  data
);
sentSignal.awaitReply(60000).then((data) => {
  console.log(data);
});
```

### Ending a Signal

Its important that you end a signal after you are done with it, it is optional but very recommended as it frees up space from your database.

```javascript
const sentSignal = await client.sendSignal(
  "SIGNAL_ID",
  "TARGET_CLIENT_ID",
  data
);
sentSignal.awaitReply(60000).then((data) => {
  console.log(data);
});
sentSignal.end();
```

OR

```javascript
client.on("signal", (signal) => {
  // do stuff
  signal.reply("accept", "good!");
  // OR
  signal.reply("error", "invalid yada yada");
  signal.end();
});
```

## Explaining Some Terms

| Term               | Meaning                                                                                                                      |
| ------------------ | ---------------------------------------------------------------------------------------------------------------------------- |
| CLIENT\_ID         | This is used so signals can be targetted to only the project(s) with that client id                                          |
| TARGET\_CLIENT\_ID | You set this in the signal so the signal only goes to the projects with that client id                                       |
| SIGNAL\_ID         | This is a custom id that is passed when sending the the signal so the receiving end knows which type of signal its receiving |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://signalz.js.org/master.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
