Quick start guide
This guide will help you integrate PUBQ's real-time platform into your application.

Quick start guide

PUBQ SDKs are available in various programming languages, providing flexibility for developers. These SDKs offer both real-time and REST interfaces or solely REST interfaces, depending on the specific SDK. You can find the list of available SDKs on the PUBQ SDK downloads page. To determine the optimal choice between the real-time and REST interfaces, refer to the guide on how to use PUBQ.

You’re currently viewing the docs in JavaScript. If you would like to view it in another language, use the selector above to change it.

Create a free PUBQ account

First, Sign up for a free account to get your own application API keys.

Get your API keys

Create an application in PUBQ Dashboard and then navigate to Keys tab. copy your application API key.

Add the PUBQ SDK

The PUBQ JavaScript SDK is available via CDN.

To get started with your project, include the SDK within the <head> of an HTML page:

<script src="https://cdn.jsdelivr.net/npm/pubq@latest/build/browser/pubq.min.js"></script>

The PUBQ JavaScript SDK is also available as an NPM module.

npm i pubq

Or using Yarn:

yarn add pubq

Then import it as ES module:

import { Pubq } from "pubq";

Connect to PUBQ

Clients need to authenticate with PUBQ to establish a realtime connection, typically over WebSockets. The following code sample prints the message Connected to PUBQ! when you’ve successfully connected.

Note: The connection example uses basic authentication to pass the API key from the application to PUBQ. Basic authentication should not be used client-side in a production environment, such as in a browser, to avoid exposing the API key. Token authentication should be used instead.

const realtime = new Pubq.RealTime({ key: "q4D9G2.rGQK1W:9pWbndVJg50MaOPAapAx7LQ_zKw6YvXl2B" });

realtime.connection.on("connected", () => {
    console.log("Connected to PUBQ!");
});

Subscribe to a channel

Messages are broadcast on channels. The following example code subscribes the client to a channel called quickstart. When a message is received, its contents will be printed after the text Received a message in realtime:.

Note: You don't need to worry about channel management. Channels are automatically created in the PUBQ Cloud when a client subscribes to them and cleaned up when they are no longer used.

let channel = realtime.channels.get("quickstart");

channel.subscribe((message) => {
    console.log("Received a message in realtime: " + message);
});

Publish a message

The following example code publishes a message using REST interface to the quickstart channel with the contents Hello!.

Note: The REST interface constructor uses the application secret to authenticate publish message request to PUBQ. The application secret should not be used client-side in a production environment, such as in a browser, to avoid exposing the application secret. Usually the REST interface be used on trusted devices like servers.

channel.publish("Hello!");

Disconnect from PUBQ

A realtime connection to PUBQ can be closed once it is no longer needed.

The following code sample explicitly disconnects from PUBQ and prints the message Closed the connection to PUBQ..

realtime.connection.close();

realtime.connection.on("closed", () => {
    console.log("Closed the connection to PUBQ.");
});
NEXT PAGE
Getting Started: Available SDKs
Lists the available PUBQ SDKs.
Start Building with PUBQ
Sign up for free
No credit card required