-
-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
improve jsonrpc js docs #6168
Open
Simon-Laux
wants to merge
9
commits into
main
Choose a base branch
from
simon/improve-jsonrpc-js-docs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
improve jsonrpc js docs #6168
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5f7ca4f
add alias handling
Septias 1c30daf
change export name from `DeltaChat` to `WebsocketDeltaChat`
Simon-Laux 77cd5b6
export `Event` and `ContextEvents` type to include them in the docume…
Simon-Laux d2fa6c4
dedicated readme for `@deltachat/jsonrpc-client`. so there is somethi…
Simon-Laux 35168ee
update link to echo bot in deltachat-rpc-server
Simon-Laux f302ce9
update deltachat-jsonrpc readme
Simon-Laux aff063e
run prettier formatter on some readme files
Simon-Laux 85d5b7b
add instructions on how to use on an unsupported platform
Simon-Laux 6ab5c4f
adapt Getting Started tutorial from cffi documentation
Simon-Laux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
# @deltachat/jsonrpc-client | ||
|
||
This package is a client for the jsonrpc server. | ||
|
||
> If you are looking for the functions in the documentation, they are under [`RawClient`](https://js.jsonrpc.delta.chat/classes/RawClient.html). | ||
|
||
### Important Terms | ||
|
||
- [delta chat core](https://github.com/deltachat/deltachat-core-rust/) the heart of all Delta Chat clients. Handels all the heavy lifting (email, encryption, ...) and provides an easy api for bots and clients (`getChatlist`, `getChat`, `getContact`, ...). | ||
- [jsonrpc](https://www.jsonrpc.org/specification) is a json based protocol | ||
for applications to speak to each other by [remote procedure calls](https://en.wikipedia.org/wiki/Remote_procedure_call) (short RPC), | ||
which basically means that the client can call methods on the server by sending a json messages. | ||
- [`deltachat-rpc-server`](https://github.com/deltachat/deltachat-core-rust/tree/main/deltachat-rpc-server) provides the jsonrpc api over stdio (stdin/stdout) | ||
- [`@deltachat/stdio-rpc-server`](https://www.npmjs.com/package/@deltachat/stdio-rpc-server) is an easy way to install `deltachat-rpc-server` from npm and use it from nodejs. | ||
|
||
#### Transport | ||
|
||
You need to connect this client to an instance of deltachat core via a transport. | ||
|
||
Currently there are 2 transports available: | ||
|
||
- (recomended) `StdioTransport` usable from `StdioDeltaChat` - speak to `deltachat-rpc-server` directly | ||
- `WebsocketTransport` usable from `WebsocketDeltaChat` | ||
|
||
You can also make your own transport, for example deltachat desktop uses a custom transport that sends the json messages over electron ipc. | ||
Just implement your transport based on the `Transport` interface - look at how the [stdio transport is implemented](https://github.com/deltachat/deltachat-core-rust/blob/7121675d226e69fd85d0194d4b9c4442e4dd8299/deltachat-jsonrpc/typescript/src/client.ts#L113) for an example, it's not hard. | ||
|
||
## Usage | ||
|
||
> The **minimum** nodejs version for `@deltachat/stdio-rpc-server` is `16` | ||
|
||
``` | ||
npm i @deltachat/stdio-rpc-server @deltachat/jsonrpc-client | ||
``` | ||
|
||
```js | ||
import { startDeltaChat } from "@deltachat/stdio-rpc-server"; | ||
// Import constants you might need later | ||
import { C } from "@deltachat/jsonrpc-client"; | ||
|
||
async function main() { | ||
const dc = await startDeltaChat("deltachat-data"); | ||
console.log(await dc.rpc.getSystemInfo()); | ||
dc.close(); | ||
} | ||
main(); | ||
``` | ||
|
||
For a more complete example refer to <https://github.com/deltachat-bot/echo/tree/master/nodejs_stdio_jsonrpc>. | ||
|
||
### Listening for events | ||
|
||
```ts | ||
dc.on("Info", (accountId, { msg }) => | ||
console.info(accountId, "[core:info]", msg) | ||
); | ||
// Or get an event emitter for only one account | ||
const emitter = dc.getContextEvents(accountId); | ||
emitter.on("IncomingMsg", async ({ chatId, msgId }) => { | ||
const message = await dc.rpc.getMessage(accountId, msgId); | ||
console.log("got message in chat " + chatId + " : ", message.text); | ||
}); | ||
``` | ||
|
||
### Getting Started | ||
|
||
This section describes how to handle the Delta Chat core library over the jsonrpc bindings. | ||
For general information about Delta Chat itself, | ||
see <https://delta.chat> and <https://github.com/deltachat>. | ||
|
||
Let's start. | ||
|
||
First of all, you have to start the deltachat-rpc-server process. | ||
|
||
```js | ||
import { startDeltaChat } from "@deltachat/stdio-rpc-server"; | ||
const dc = await startDeltaChat("deltachat-data"); | ||
``` | ||
|
||
Then we have to create an Account (also called Context or profile) that is bound to a database. | ||
The database is a normal SQLite file with a "blob directory" beside it. | ||
But these details are handled by deltachat's account manager. | ||
So you just have to tell the account manager to create a new account: | ||
|
||
```js | ||
const accountId = await dc.rpc.addAccount(); | ||
``` | ||
|
||
After that, register event listeners so you can see what core is doing: | ||
Intenally `@deltachat/jsonrpc-client` implments a loop that waits for new events and then emits them to javascript land. | ||
```js | ||
dc.on("Info", (accountId, { msg }) => | ||
console.info(accountId, "[core:info]", msg) | ||
); | ||
``` | ||
|
||
Now you can **configure the account:** | ||
```js | ||
// use some real test credentials here | ||
await dc.rpc.setConfig(accountId, "addr", "[email protected]") | ||
await dc.rpc.setConfig(accountId, "mail_pw", "***") | ||
// you can also set multiple config options in one call | ||
await dc.rpc.batchSetConfig(accountId, { | ||
"addr": "[email protected]", | ||
"mail_pw": "***" | ||
}) | ||
|
||
// after setting the credentials attempt to login | ||
await dc.rpc.configure(accountId) | ||
``` | ||
|
||
`configure()` returns a promise that is rejected on error (with await is is thrown). | ||
The configuration itself may take a while. You can monitor it's progress like this: | ||
```js | ||
dc.on("ConfigureProgress", (accountId, { progress, comment }) => { | ||
console.log(accountId, "ConfigureProgress", progress, comment); | ||
}); | ||
// make sure to register this event handler before calling `dc.rpc.configure()` | ||
``` | ||
|
||
The configuration result is saved in the database. | ||
On subsequent starts it is not needed to call `dc.rpc.configure(accountId)` | ||
(you can check this using `dc.rpc.isConfigured(accountId)`). | ||
|
||
On a successfully configuration delta chat core automatically connects to the server, however subsequent starts you **need to do that manually** by calling `dc.rpc.startIo(accountId)` or `dc.rpc.startIoForAllAccounts()`. | ||
|
||
```js | ||
if (!await dc.rpc.isConfigured(accountId)) { | ||
// use some real test credentials here | ||
await dc.rpc.batchSetConfig(accountId, { | ||
"addr": "[email protected]", | ||
"mail_pw": "***" | ||
}) | ||
await dc.rpc.configure(accountId) | ||
} else { | ||
await dc.rpc.startIo(accountId) | ||
} | ||
``` | ||
|
||
Now you can **send the first message:** | ||
|
||
```js | ||
const contactId = await dc.rpc.createContact(accountId, "[email protected]", null /* optional name */) | ||
const chatId = await dc.rpc.createChatByContactId(accountId, contactId) | ||
|
||
await dc.rpc.miscSendTextMessage(accountId, chatId, "Hi, here is my first message!") | ||
``` | ||
|
||
`dc.rpc.miscSendTextMessage()` returns immediately; | ||
the sending itself is done in the background. | ||
If you check the testing address (bob), | ||
you should receive a normal e-mail. | ||
Answer this e-mail in any e-mail program with "Got it!", | ||
and the IO you started above will **receive the message**. | ||
|
||
You can then **list all messages** of a chat as follows: | ||
|
||
```js | ||
let i = 0; | ||
for (const msgId of await exp.rpc.getMessageIds(120, 12, false, false)) { | ||
i++; | ||
console.log(`Message: ${i}`, (await dc.rpc.getMessage(120, msgId)).text); | ||
} | ||
``` | ||
|
||
This will output the following two lines: | ||
``` | ||
Message 1: Hi, here is my first message! | ||
Message 2: Got it! | ||
``` | ||
|
||
<!-- TODO: ### Clean shutdown? - seems to be more advanced to call async functions on exit, also is this needed in this usecase? --> | ||
|
||
## Further information | ||
|
||
- `@deltachat/stdio-rpc-server` | ||
- [package on npm](https://www.npmjs.com/package/@deltachat/stdio-rpc-server) | ||
- [source code on github](https://github.com/deltachat/deltachat-core-rust/tree/main/deltachat-rpc-server/npm-package) | ||
- [use `@deltachat/stdio-rpc-server` on an usuported platform](https://github.com/deltachat/deltachat-core-rust/tree/main/deltachat-rpc-server/npm-package#how-to-use-on-an-unsupported-platform) | ||
- The issue-tracker for the core library is here: <https://github.com/deltachat/deltachat-core-rust/issues> | ||
|
||
If you need further assistance, | ||
please do not hesitate to contact us | ||
through the channels shown at https://delta.chat/en/contribute | ||
|
||
Please keep in mind, that your derived work | ||
must respect the Mozilla Public License 2.0 of deltachat-rpc-server | ||
and the respective licenses of the libraries deltachat-rpc-server links with. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,20 +18,46 @@ import { startDeltaChat } from "@deltachat/stdio-rpc-server"; | |
import { C } from "@deltachat/jsonrpc-client"; | ||
|
||
async function main() { | ||
const dc = await startDeltaChat("deltachat-data"); | ||
console.log(await dc.rpc.getSystemInfo()); | ||
dc.close() | ||
const dc = await startDeltaChat("deltachat-data"); | ||
console.log(await dc.rpc.getSystemInfo()); | ||
dc.close(); | ||
} | ||
main() | ||
main(); | ||
``` | ||
|
||
For a more complete example refer to https://github.com/deltachat-bot/echo/pull/69/files (TODO change link when pr is merged). | ||
For a more complete example refer to https://github.com/deltachat-bot/echo/tree/master/nodejs_stdio_jsonrpc. | ||
|
||
## How to use on an unsupported platform | ||
|
||
<!-- todo instructions, will uses an env var for pointing to `deltachat-rpc-server` binary --> | ||
You need to have rust installed to compile deltachat core for your platform and cpu architecture. | ||
<https://rustup.rs/> is the recommended way to install rust. | ||
Also your system probably needs more than 4gb ram to compile core, alternatively your could try to build the debug build, that might take less ram to build. | ||
|
||
<!-- todo copy parts from https://github.com/deltachat/deltachat-desktop/blob/7045c6f549e4b9d5caa0709d5bd314bbd9fd53db/docs/UPDATE_CORE.md --> | ||
1. clone the core repo, right next to your project folder: `git clone [email protected]:deltachat/deltachat-core-rust.git` | ||
2. go into your core checkout and run `git pull` and `git checkout <version>` to point it to the correct version (needs to be the same version the `@deltachat/jsonrpc-client` package has) | ||
3. run `cargo build --release --package deltachat-rpc-server --bin deltachat-rpc-server` | ||
|
||
Then you have 2 options: | ||
|
||
### point to deltachat-rpc-server via direct path: | ||
|
||
```sh | ||
# start your app with the DELTA_CHAT_RPC_SERVER env var | ||
DELTA_CHAT_RPC_SERVER="../deltachat-core-rust/target/release/deltachat-rpc-server" node myapp.js | ||
``` | ||
|
||
### install deltachat-rpc-server in your $PATH: | ||
|
||
```sh | ||
# use this to install to ~/.cargo/bin | ||
cargo install --release --package deltachat-rpc-server --bin deltachat-rpc-server | ||
# or manually move deltachat-core-rust/target/release/deltachat-rpc-server | ||
# to a location that is included in your $PATH Environment variable. | ||
``` | ||
|
||
```js | ||
startDeltaChat("data-dir", { takeVersionFromPATH: true }); | ||
``` | ||
|
||
## How does it work when you install it | ||
|
||
|
@@ -46,7 +72,7 @@ references: | |
When you import this package it searches for the rpc server in the following locations and order: | ||
|
||
1. `DELTA_CHAT_RPC_SERVER` environment variable | ||
2. use the PATH when `{takeVersionFromPATH: true}` is supplied in the options. | ||
2. use the PATH when `{takeVersionFromPATH: true}` is supplied in the options. | ||
3. prebuilds in npm packages | ||
|
||
so by default it uses the prebuilds. | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will be shown as start page for https://js.jsonrpc.delta.chat