JavaScript Data API

The JavaScript Data API lets you connect to your TalkJS chat as a user and read, subscribe to, and update your chat data. It's available as part of our regular JavaScript SDK and as a separate npm package, @talkjs/core.

The JavaScript Data API uses WebSockets to create a connection to the TalkJS servers. It is primarily intended to be used in the browser because it connects as a single specific user. However, you can also use it in Node.js. See Node.js compatibility for more details.

How to install TalkJS Core

You can install the @talkjs/core npm package, or include it as a script.

Install with a package manager

Install the @talkjs/core npm package:

1npm install @talkjs/core

Include as a script

If you use TalkJS without a build step, you can include TalkJS core as a script.

Add the following to the <head> tag of the HTML page where you want to use TalkJS Core:

1<script type="module">
2 import { getTalkSession } from 'https://cdn.jsdelivr.net/npm/@talkjs/[email protected]';
3</script>

Node.js compatibility

If you use TalkJS Core 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.

If you are using an older version of Node.js without built-in support for WebSockets, you will need to add support with a library. We recommend installing the ws package. Then tell @talkjs/core to use WebSocket from ws:

1import { getTalkSession, registerPolyfills } from "@talkjs/core";
2import { WebSocket } from "ws";
3
4registerPolyfills({ WebSocket: WebSocket });
5
6const session = getTalkSession({ ... });