@talkjs/core 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1 @@
1
+ This is proprietary software. See Terms & Conditions here: https://talkjs.com/terms
package/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # TalkJS Core
2
+
3
+ The `@talkjs/core` package lets you connect to your [TalkJS](https://talkjs.com) chat as a user and read, subscribe to, and update your chat data.
4
+
5
+ [See our docs](https://talkjs.com/docs/Reference/JavaScript_Chat_SDK/Realtime_API/) for more information.
6
+
7
+ ## Prerequisites
8
+
9
+ To use this package, you will need a [TalkJS account](https://talkjs.com/dashboard/login). TalkJS provides a ready-to-use chat client for your application. Your account gives you access to TalkJS's free development environment.
10
+
11
+ ## Example
12
+
13
+ This example demonstrates how to create a TalkJS session and then create two users and conversation between them.
14
+
15
+ Install the `@talkjs/core` package:
16
+
17
+ ```sh
18
+ npm install @talkjs/core
19
+ # or
20
+ yarn add @talkjs/core
21
+ ```
22
+
23
+ Import it into the component where you want to use it:
24
+
25
+ ```js
26
+ import { TalkSession } from "@talkjs/core";
27
+ ```
28
+
29
+ Then add the following code to create the session, users and conversation:
30
+
31
+ ```js
32
+ // Replace with your own app ID
33
+ const appId = "<APP_ID>";
34
+ const userId = "alice";
35
+
36
+ const session = new TalkSession({ appId, userId });
37
+ session.currentUser.createIfNotExists({ name: "Alice" });
38
+
39
+ const conversation = session.conversation("my_conversation");
40
+ conversation.createIfNotExists();
41
+
42
+ conversation.subscribeMessages(messages => {
43
+ console.log(messages);
44
+ });
45
+ ```
46
+
47
+ For example, if you are using React, add this code inside a `useEffect` hook.
48
+
49
+ ## Node.js compatibility
50
+
51
+ This package is primarily intended to be used in the browser because it only connects as a single user. However, it also works in Node.js.
52
+
53
+ The `@talkjs/core` package uses `WebSocket`s to create a connection to the TalkJS servers. If you use `@talkjs/core` in the browser or in Node.js version 22, it will use the built-in `WebSocket` implementation automatically. Node.js version 21 will also use the built-in implementation if the `--experimental-websocket` flag is enabled.
54
+
55
+ If you are using an older version of Node.js without built-in support for `WebSocket`s, you will need to add support with a library. We recommend installing the [ws](https://www.npmjs.com/package/ws) package. Then tell `@talkjs/core` to use `WebSocket` from the library:
56
+
57
+ ```js
58
+ import { TalkSession, registerPolyfills } from "@talkjs/core";
59
+ import { WebSocket } from "ws";
60
+
61
+ registerPolyfills({ WebSocket: WebSocket });
62
+
63
+ const session = new TalkSession(...);
64
+ ```
65
+
66
+ ## Support
67
+
68
+ If you encounter any problems with `@talkjs/core`, please open a [chat with support](https://talkjs.com/?chat). TalkJS support is staffed by engineers.