Knock Javascript client library
A client-side Javascript library to interact with user-facing Knock features, such as feeds.
Note: this is a lower level library designed for building UI on top of
Documentation
See the documentation for usage examples.
Installation
Via NPM:
npm install @knocklabs/client
Via Yarn:
yarn add @knocklabs/client
Configuration
To configure the client library you will need:
- A public API key (found in the Knock dashboard)
- A feed channel ID (found in the Knock dashboard)
- A user ID, and optionally an auth token for production environments
import Knock from "@knocklabs/client";
const knockClient = new Knock(process.env.KNOCK_API_KEY);
knockClient.authenticate(
currentUser.id,
currentUser.knockToken,
);
Retrieving new items from the feed
import Knock from "@knocklabs/client";
const knockClient = new Knock(process.env.KNOCK_API_KEY);
knockClient.authenticate(currentUser.id, currentUser.knockToken);
const feedClient = knockClient.feeds.initialize(
process.env.KNOCK_FEED_CHANNEL_ID,
);
feedClient.listenForUpdates();
feedClient.on("items.received.page", ({ items }) => {
console.log(items);
});
feedClient.on("items.received.realtime", ({ items }) => {
console.log(items);
});
feedClient.on("items.received.*", ({ items }) => {
console.log(items);
});
feedClient.fetch({
status: "all" | "unread" | "unseen",
after: lastItem.__cursor,
before: firstItem.__cursor,
page_size: 10,
source: "notification-key",
tenant: "jurassic-park",
});
feedClient.teardown();
Reading the feed store state (programmatically)
const feedClient = knockClient.feeds.initialize(
process.env.KNOCK_FEED_CHANNEL_ID,
);
const { items } = feedClient.store.getState();
Reading the feed store state (in React)
import create from "zustand";
const feedClient = knockClient.feeds.initialize(
process.env.KNOCK_FEED_CHANNEL_ID,
);
const useFeedStore = create(feedClient.store);
const items = useFeedStore((state) => state.items);
const meta = useFeedStore((state) => state.metadata);
Marking items as read, seen, or archived
const feedClient = knockClient.feeds.initialize(
process.env.KNOCK_FEED_CHANNEL_ID,
);
feedClient.markAsRead(feedItemOrItems);
feedClient.markAsSeen(feedItemOrItems);
feedClient.markAsArchived(feedItemOrItems);
feedClient.markAsUnread(feedItemOrItems);
feedClient.markAsUnseen(feedItemOrItems);
feedClient.markAsUnarchived(feedItemOrItems);
Managing user preferences
await knockClient.user.setPreferences({
channel_types: { email: true, sms: false },
workflows: {
"dinosaurs-loose": {
channel_types: { email: false, in_app_feed: true },
},
},
});
const preferences = await knockClient.user.getPreferences();
const preferenceSets = await knockClient.user.getAllPreferences();
Managing the current user's channel data
const channelData = await knockClient.user.getChannelData({
channelId: "uuid-knock-channel-id",
});
await knockClient.user.setChannelData({
channelId: "uuid-knock-channel-id",
channelData: {
tokens: ["apns-user-push-token"],
},
});
See provider requirements for setting channel data here.
Automatically disconnect sockets from inactive tabs
Optionally, you can configure the client to disconnect socket connections with inactive tabs after a brief delay. If the tab becomes active again, the socket will reconnect to continue receiving real-time notification updates.
const feedClient = knockClient.feeds.initialize(
process.env.KNOCK_FEED_CHANNEL_ID,
{
auto_manage_socket_connection: true,
auto_manage_socket_connection_delay: 2500,
},
);