- restrict access to private Channels
- forcefully subscribe users to Channels
- authenticate and identify users connected to Channels
- send direct messages to users (instead of to entire Channels)
Do I need to use Tokens?
Tokens are not necessary in every application. Some applications may choose to use Tokens for logged in users only, or even not at all - it really depends on your use case. If you’re providing data that you want everyone to be able to see (without signing into your app, for example) then it probably makes the most sense to use anunprotected
channel, which can be subscribed to using our frontend SDKs
without using Tokens.
A good use-case for Tokens would be if you’re building something like a group
messaging app, for example, and need to limit access to certain channels, such
as private group chat channels, to specific users.
Server-side Examples
The examples below assume you have already set up the Hop server-side
SDK.
Generating Tokens
You can generate Tokens manually (using our Console) or via our SDKs & API. Here’s an example of generating tokens using the server-side SDK:create-token.js
Subscribing Tokens to Channels
When you subscribe a Token to a channel, it will be subscribed to that channel in realtime and it will be made available to any live connections using that Token. In this example, let’s assume that we have a private channel with the IDgame
.
subscribe-token.js
Sending Messages Directly to Tokens
send-message-to-token.js
Determine if a Token is Online
You can use theisOnline
function to check if a Token is currently connected
to Channels.
is-token-online.js
Client-side Examples
Using Tokens with Client-side SDKs
Implementing Tokens into your frontend app is super easy. Just pass it intohop.init
in the token
key. For example:
index.js