Why Chat SDK?
Our overall goal behind creating this Chat SDK was fine-tuning the developer experience of implementing a chat with PubNub over the regular JavaScript SDK. We know that using a universal SDK for a specific solution, such as chat, can be a lengthy trial-and-error process that takes time and resources. To make that process easier, we created this Chat SDK with a few assumptions in mind.
Speak Chat
If you're looking for a solution to add chat to your app, you'd expect this solution to use chat-specific naming. This means you want "users" to be able to send "messages" to a set of specific "channels." You also want them to add message reactions, see the typing indicator, bundle messages into threads, mention other users, and many more.
Chat SDK uses chat-specific terminology for object and method naming - its goal is to let you instantly know what you can do where and how:
- If you want to create a new user, initialize chat and call
createUser()
on theChat
object. - Do you want to turn on the typing indicator whenever someone in the chat room starts writing a message? Use
startTyping()
. - Or maybe you want to let users flag an offensive message and send it to the admin for review? Call
report()
on theMessage
object.
Simple as that.
Hide complexity
A huge advantage of PubNub APIs is that they are very flexible - you can use each API to create different features in different use cases. What if you have a single chat use case in mind and just want to get things done?
Chat SDK gives definite and immediate answers.
Let's look at the example of a typing indicator. You could code it using PubNub Signals. But then, you must define the payload of the messages that will communicate typing events and reuse that payload for every platform implementation of your application. Doing updates, in this case, would take a lot of work.
With Chat SDK, the startTyping()
method would make further calls to any internal service we deem necessary. A higher level of abstraction we use in the Chat SDK allows us to hide the underlying complexity and lets you save time trying to figure things out.
Focus on solutions, not APIs
API Reference documentation of other SDKs lists all available APIs, letting you build your solutions from the functional building blocks. It's ok to use a single service, such as Pub/Sub API, for example, to let tens or hundreds of IoT devices communicate their readings frequently through some high-performance messaging system. There's probably no need to associate metadata with users, channels, or memberships. We just need readings, so the use of messaging alone is perfect.
Most chat applications, however, require multiple services at once. Developers would have to start their chat applications by “connecting” users to channels, either by “creating” them or “getting” their info from the server (App Context API). You would like users to be able to "join" channels (App Context API), "receive" (Subscribe API), "send" (Publish API), and "store" messages at PubNub (Message Persistence API).
Chat SDK aims to hide this separation of various PubNub APIs and provide solutions for the features you'd look for. This way, you can build chat using clearly named methods called on clearly named objects, without worrying about which PubNub APIs are called underneath. For example, you can use invite()
on the Channel
object and provide the user who you want to invite as a parameter. Similarly, the Chat SDK docs are divided by logical features rather than APIs and objects. For example, when you want to implement threads, you will look for the related info under the Messages
section, and so on.
Rely on TypeScript
A full-featured chat could require a dozen or more different API calls. Using JavaScript, you must pass the same data, such as the channel ID, as an argument to every call.
As an alternative to that, the Chat SDK is written in TypeScript. It follows the object-oriented programming approach, providing clear entities like User
, Message
, or Channel
to interact with. You can modify these entities independently without affecting others, which makes them easier to maintain.
Using objects and classes reduces development time and allows reuse of existing code. If an object or class already exists, you can inherit from them, simplifying the program and reducing the chances of errors.
Finally, all entities and methods are statically typed, so when you start writing channel.
in your IDE, you'll automatically get suggestions based on what is possible to do within the channel's scope.
Leave room for flexibility
Our SDK is universal and not tied to any specific framework. You can choose the framework that best suits your needs or the one they are already familiar with, be it React, React Native, Angular, Vue.js, or Svelte.
Grease the wheel, don't reinvent it
Chat SDK aims to improve the developer experience when creating a chat with PubNub. It relies heavily on the existing JavaScript SDK for all underlying calls while providing the improvements mentioned in the previous points.
When you initialize the Chat SDK (init()
), the @pubnub/chat
PubNub package gets added as a dependency to the package.json
file in your project. This package contains the pubnub
dependency, which stands for the JavaScript SDK.
You get immediate access to all JavaScript SDK methods after initializing the Chat SDK. The underlying JavaScript SDK is exposed as the sdk
property of the Chat SDK instance. Use the sdk
property to access any JavaScript SDK API and method:
const chat = await Chat.init({
publishKey: "yourPublishKey",
subscribeKey: "yourSubscribeKey",
userId,
typingTimeout: 2000,
})
// Use the "sdk" property to access the App Context API ("objects") and its "getAllChannelMetadata()" method
chat.sdk.objects.getAllChannelMetadata()
If the Chat SDK uses JavaScript methods underneath, what's improved, then? Let's look at the example of threads. You could create the related logic yourself after figuring out how to connect bits and pieces from the Message Persistence, App Context, and Message Reactions APIs. Chat SDK does that for you by providing easy methods for creating and managing threads that call the JavaScript SDK methods underneath.
Take the server load off your shoulders
Chat SDK is a purely client-side library that serves as the bridge between your chat app and the feature-rich PubNub server infrastructure.
Suppose you want to develop a client chat app using the Chat SDK. In that case, you'll need to provide your own server (or service) to authenticate your users and authorize them to use PubNub resources (users and channels). For other features, however, you can rely entirely on the PubNub server and its built-in APIs to implement such real-time features as:
- Sending and receiving messages (Pub/Sub API)
- User presence and state (Presence API)
- Message storage (Message Persistence)
- User data management (App Context API)
Aside from being feature-rich, PubNub ensures that the chat apps you create are fast, scalable, and secure:
- Low latency - PubNub ensures that any message your chat users will send gets delivered anywhere in the world in under 100ms. PubNub has a global network of data centers known as Points of Presence (PoPs) that minimize the distance that data needs to travel.
- High availability - PubNub commits to 99.999% uptime SLA.
- Scalability - PubNub is designed to handle large numbers of concurrent connections and messages.
- Security - PubNub is GDPR- and HIPAA-compliant.
By choosing PubNub, you take the above burden off your shoulders and have PubNub handle it for you.