API Reference (@knocklabs/client)
In this section, you'll find the documentation for the classes and methods available in the @knocklabs/client
library.
Knock
The top level Knock
class, used to interact with a client instance.
Params:
Example:
userId
Returns the user ID of the authenticated user.
Returns: string
feeds
Returns a FeedClient
instance that can be initialized to return a feed.
Returns: Feed
instance
Example:
preferences
Returns a Preferences
instance to interact with the preferences API for the current, authenticated user.
Returns: Preferences
instance
Example:
client
Returns an instance of an authenticated ApiClient
that can be used to make HTTP and Websocket requests to Knock.
Returns: ApiClient
instance
authenticate
Authenticates the current user and creates a new Knock session.
Params:
Returns: void
Example:
teardown
Tearsdown a current session and disconnects any connected sockets.
Returns: void
ApiClient
The API client exposes direct functions for communicating with the Knock API over HTTP and websocket.
socket
Returns: a Socket
connectSocket
Connects the socket instance to Knock's realtime server.
Returns: void
disconnectSocket
Disconnects the socket from Knock's realtime server.
Returns: void
makeRequest
Feed
Represents the connection between a user and a feed, including methods for interacting with the items on that feed. Also includes a stateful store that can be used to build in-app notification experiences easily. See FeedStoreState
for more on the shape of the store.
store
Returns: StoreApi<FeedStoreState>
listenForUpdates
Connects the feed instance to the realtime socket so that any new items published to the feed are received over the websocket.
Returns: a teardown function.
Example:
Returns: void
on
Binds an event handler to be invoked when the event is triggered.
Events:
Params:
Returns: void
.
Example:
off
Unbinds an existing event handler previously bound with on
. Use this method to cleanup bound event handlers.
Returns: void
.
getState
Programatically access the current FeedStoreState
.
Returns: FeedStoreState
.
markAsSeen
Marks the given items as seen
. Will perform the operation optimistically, including updating the current metadata
in the FeedStoreState
.
Params:
Returns: Promise<ApiResponse>
markAsUnseen
Removes the seen
status on the item or items given. Will perform the operation optimistically, including updating the current metadata
in the FeedStoreState
.
Params:
Returns: Promise<ApiResponse>
markAsRead
Sets the read
status on the item or items given. Will perform the operation optimistically, including updating the current metadata
in the FeedStoreState
.
Params:
Returns: Promise<ApiResponse>
markAsUnread
Removes the read
status on the item or items given. Will perform the operation optimistically, including updating the current metadata
in the FeedStoreState
.
Params:
Returns: Promise<ApiResponse>
markAsArchived
Sets the archived
status on the item or items given. Will perform the operation optimistically, including updating the current metadata
in the FeedStoreState
.
Params:
Returns: Promise<ApiResponse>
markAsUnarchived
Removes the archived
status on the item or items given. Will perform the operation optimistically, including updating the current metadata
in the FeedStoreState
.
Params:
Returns: Promise<ApiResponse>
fetch
Fetches
Emits items.received.page
events on a successful fetch.
Params:
Returns: Promise<ApiResponse>
fetchNextPage
Fetches the next page of the feed items (if there are any more to fetch). Note: this will
FeedStoreState
A zustand state store that holds the current feed state, including item counts, for easy notification feed rendering within your application. The FeedStoreState is entirely managed by the Feed
instance, and any calls to fetch()
, markAsX
, or fetchNextPage
will update the state accordingly.
Preferences
get
Retrieves a preference set for the authenticated user by calling the get preferences endpoint directly from the client.
Returns: Promise<ApiResponse>
Example:
set
Updates the authenticated users preferences by calling the set preferences endpoint directly from the client.
Params:
Returns: Promise<ApiResponse>
Example: