# EdgeMatrix JS Monorepo

This is project reference `ethereumjs-monorepo`

### Packages

| package                                                                                                    | npm                                                               | keywords                                         |
| ---------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------- | ------------------------------------------------ |
| [@edgematrixjs/util](https://github.com/EMCProtocol-dev/edgematrixjs-monorepo/blob/main/packages/util)     | [npm package](https://www.npmjs.com/package/@edgematrixjs/util)   | `utility` `create private key`                   |
| [@edgematrixjs/tx](https://github.com/EMCProtocol-dev/edgematrixjs-monorepo/blob/main/packages/tx)         | [npm package](https://www.npmjs.com/package/@edgematrixjs/tx)     | `rlp encode` `keccak256 sign`                    |
| [@edgematrixjs/socket](https://github.com/EMCProtocol-dev/edgematrixjs-monorepo/blob/main/packages/socket) | [npm package](https://www.npmjs.com/package/@edgematrixjs/socket) | `websocket`                                      |
| [@edgematrixjs/http](https://github.com/EMCProtocol-dev/edgematrixjs-monorepo/blob/main/packages/http)     | [npm package](https://www.npmjs.com/package/@edgematrixjs/http)   | `http`                                           |
| [@edgematrixjs/rtc](https://github.com/EMCProtocol-dev/edgematrixjs-monorepo/blob/main/packages/rtc)       | [npm package](https://www.npmjs.com/package/@edgematrixjs/rtc)    | `create subject` `scribe subject` `send message` |
| [@edgematrixjs/client](https://github.com/EMCProtocol-dev/edgematrixjs-monorepo/blob/main/packages/client) | [npm package](https://www.npmjs.com/package/@edgematrixjs/client) | `edgematrixjs for browser`                       |

### Basic Usage

### CommonJS and ESmodules

```
npm install --save @edgematrixjs/rtc
```

**Create Subject**

```
import { Http } from '@edgematrixjs/http';
import { RTC } from '@edgematrixjs/rtc';
const chatId = 2;
const privateKey = '';
const httpsUrl = 'https://oregon.edgematrix.xyz';
const emHttp = new Http({ baseURL: httpsUrl });
const rtc = new RTC();
const { _result, hash } = await rtc.createSubject(chainId, privateKey, emHttp);
//_result === 0 is success, the 'hash' is subject
```

**Subscribe Subject**

```
import { EmSocket } from '@edgematrixjs/socket';
import { RTC } from '@edgematrixjs/rtc';
//connect method implement follow in test/index.spec.ts
const wssUrl = 'wss://oregon.edgematrix.xyz/edge_ws';
const handleAction = ({ action, event }) => {};
const { _result: _socketResult, emSocket, event } = await connect({ network: wssUrl, callback: handleAction });
if (_socketResult !== 0) {
  throw new Error('socket is error');
}
const rtc = new RTC();
const chatId = 2;
const application = 'edge_chat';
const subject = 'Your Subject';
const content = 'Your Content';
const params = { subject, application, content, chainId };
const { _result } = await rtc.subscribe(params, privateKey, emSocket);
//_result === 0 is success
```

**Send Message**

```
import { Http } from '@edgematrixjs/http';
import { RTC } from '@edgematrixjs/rtc';
const httpsUrl = 'https://oregon.edgematrix.xyz';
const emHttp = new Http({ baseURL: httpsUrl });
const rtc = new RTC();
const params = {
  subject: globalSubject,
  application: 'edge_chat',
  content: JSON.stringify({ data: 'test send message' }),
  //When the "To" parameter is empty, everyone who subscribed to the subject will receive your message
  //to?: {TargetPublicKey},
  chainId,
};
const { _result } = await rtc.sendMessage(params, privateKey, emHttp);
//_result === 0 is success
```

### Browser

```
<script src="packages/client/dist/bundle.js"></script>
```

```
<script>
  const privateKey = '0xb22be9c19b61adc1d8e89a1dae0346ed274ac9fa239c06286910c29f9fee59d3';
  const subject = '0x8eeb338239ada22d81ffb7adc995fe31a4d1dc2d701bc8a58fffe5b53e14281e';
  const httpsUrl = 'https://oregon.edgematrix.xyz';
  const wssUrl = 'wss://oregon.edgematrix.xyz/edge_ws';

  window.onload = function () {
    const Http = edgematrixjs.Http;
    const RTC = edgematrixjs.RTC;
    const ws = new edgematrixjs.EmSocket({ url: wssUrl });
    //set open listener
    ws.setOpenListener((event) => {
      const rtc = new RTC();
      const subscribe = { subject: subject, application: 'edge_chat', content: 'subject', chainId: 2 };

      rtc.subscribe(subscribe, privateKey, ws).then((resp) => {
        //send message
        const http = new Http({ baseURL: httpsUrl });
        const messageContent = { data: 'hello' };
        const message = {
          subject: subject,
          application: 'edge_chat',
          chainId: 2,
          content: JSON.stringify(messageContent),
        };
        rtc.sendMessage(message, privateKey, http);
      });
    });

    //set message listener
    ws.addMessageListener((event) => {
      const data = JSON.parse(event.data) || {};
      const params = data.params || {};
      const result = params.result || {};
      const from = result.From;
      const type = result.Type;
      let content = { data: '' };
      try {
        content = JSON.parse(result.Content);
      } catch (e) {}
      console.info(`recerved message `, content);
    });
    //connect socket
    ws.connect();
  };
</script>
```


---

# 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://docs.emc.network/emc/openverse/edgematrix-js-monorepo.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.
