Dart API & SDK Docs 5.2.0
In this guide, we'll create a simple "Hello, World" application that demonstrates the core concepts of PubNubPubNub
PubNub is a real-time messaging platform that provides APIs and SDKs for building scalable applications. It handles the complex infrastructure of real-time communication, including:
- Message delivery and persistence
- Presence detection
- Access control
- Push notifications
- File sharing
- Serverless processing with Functions and Events & Actions
- Analytics and monitoring with BizOps Workspace
- AI–powered insights with Illuminate
- Setting up a connection
- Sending messages
- Receiving messages in real-time
Overview
This guide will help you get up and running with PubNub in your Dart application. Since Dart is commonly used across different platforms, we provide two implementation paths:
- Flutter app development: For developers building mobile or web applications with Flutter
- Non-Flutter platforms: For developers using Dart in other environments (server-side, CLI, etc.)
The core PubNub concepts and API usage remain the same across both paths, but implementation details like lifecycle management and UI updates differ. Select the appropriate tab in each section to see platform-specific guidance.
Prerequisites
Before we dive in, make sure you have:
- A basic understanding of Dart
- Flutter SDK or Dart SDK installed
- Your preferred IDE (VS Code, Android Studio, etc.)
- A PubNub account (we'll help you set this up!)
Setup
Get your PubNub keys
First things first – you'll need your PubNub keys to get started. Here's how to get them:
- Sign in or create an account on the PubNub Admin Portal.
- Create a new app (or use an existing one).
- Find your publishand subscribe
Publish Key
A unique identifier that allows your application to send messages to PubNub channels. It's part of your app's credentials and should be kept secure.keys in the app's dashboard.Subscribe Key
A unique identifier that allows your application to receive messages from PubNub channels. It's part of your app's credentials and should be kept secure.
When you create a new app, PubNub automatically generates your first set of keys. While you can use the same keys for development and production, we recommend creating separate keysets for each environment for better security and management.
Install the SDK
SDK version
Always use the latest SDK version to have access to the newest features and avoid security vulnerabilities, bugs, and performance issues.
- Flutter app development
- Non-Flutter platforms
To integrate PubNub into your Flutter project, add this dependency to your pubspec.yaml
file:
dependencies:
pubnub: 5.2.0
flutter:
sdk: flutter
Then run:
flutter pub get
Don't forget to add internet permission:
For Android, add to your AndroidManifest.xml
:
<uses-permission android:name="android.permission.INTERNET" />
For iOS, no additional steps are needed as internet access is permitted by default.
View the supported platforms for more information about compatibility.
Steps
Initialize PubNub
- Flutter app development
- Non-Flutter platforms
In your Flutter project, add the PubNub initialization code to your main application file. This is the minimum configuration you need to send and receive messages with PubNub in your Flutter application.
Make sure to replace the demo keys with your app's publish and subscribe keys from the Admin Portal.
// Import required packages
import 'package:flutter/material.dart';
import 'package:pubnub/pubnub.dart';
class PubNubApp extends StatefulWidget {
_PubNubAppState createState() => _PubNubAppState();
}
class _PubNubAppState extends State<PubNubApp> {
// PubNub instance
late PubNub pubnub;
// Subscription for messages
late Subscription subscription;
// Channel name
show all 60 linesIn the IDE of your choice, create a new Dart file with the following content. This is the minimum configuration you need to send and receive messages with PubNub.
Make sure to replace the demo keys with your app's publish and subscribe keys from the Admin Portal.
// Import required packages
import 'package:pubnub/pubnub.dart';
// Create PubNub instance with default keyset
var pubnub = PubNub(
defaultKeyset: Keyset(
subscribeKey: 'demo', // Replace with your subscribe key
publishKey: 'demo', // Replace with your publish key
userId: UserId('myUniqueUserId'),
),
);
For more information, refer to the Configuration section of the SDK documentation.
Set up event listeners
Listeners help your app react to events and messages. You can implement custom app logic to respond to each type of message or event.
- Flutter app development
- Non-Flutter platforms
In your Flutter application, add this code to your setupPubNub()
method:
void setupPubNub() {
// Create a subscription to the channel
subscription = pubnub.subscribe(channels: {channel});
// Set up message listener
subscription.messages.listen((message) {
// Update UI with the received message
setState(() {
messages.add(message.content.toString());
});
print('Received message: ${message.content}');
});
// You can also listen for presence events
show all 19 linesNow, let's update the build
method to display messages:
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('PubNub Flutter Example'),
),
body: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: messages.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(messages[index]),
);
show all 44 linesDon't forget to add the controller:
final TextEditingController _messageController = TextEditingController();
Add the following code to set up listeners for messages:
// Define the channel
final channel = "my_channel";
// Create a subscription to the channel
final subscription = pubnub.subscribe(channels: {channel});
// Set up message listener
subscription.messages.listen((message) {
print('Received message: ${message.content}');
});
// You can also listen for presence events
subscription.presence.listen((presence) {
print('Presence event: ${presence.event}');
});
For more information, refer to the Publish and Subscribe section of the SDK documentation.
Publish messages
When you publish a message to a channel, PubNub delivers that message to everyone who is subscribed to that channel.
A message can be any type of JSON-serializable data (such as objects, arrays, integers, strings) that is smaller than 32 KiB.
- Flutter app development
- Non-Flutter platforms
Add a method to publish messages in your Flutter application:
Future<void> sendMessage(String text) async {
if (text.isEmpty) return;
try {
// Publish the message to the channel
final result = await pubnub.publish(
channel,
{'text': text, 'sender': 'flutter_user'},
);
print('Published message with timetoken: ${result.timetoken}');
} catch (e) {
print('Failed to publish message: $e');
// Show error to user
show all 20 linesHere's how to publish messages in a Dart application:
Future<void> publishMessage() async {
try {
// Message to publish
final message = {'text': 'Hello, world!', 'sender': 'dart_user'};
print('Message to send: $message');
// Publish the message to the channel
final result = await pubnub.publish(channel, message);
print('Message "${result.description}" with timetoken: ${result.timetoken}');
} catch (e) {
print('Error publishing message: $e');
}
}
show all 17 linesRun the app
- Flutter app development
- Non-Flutter platforms
To test your Flutter application:
- Launch your emulator or connect your physical device.
- Run your app with
flutter run
. - You should see the PubNub Flutter example UI appear.
- Enter a message in the text field and tap the send button.
- You should see your message appear in the list as it's received back through the subscription.
To test your Dart application, save your code to a file (e.g., pubnub_example.dart
) and run it with:
dart pubnub_example.dart
When you run the application, you should see output similar to the following:
Message to send: {text: Hello, world!, sender: dart_user}
Received message: {text: Hello, world!, sender: dart_user}
Message "Sent" with timetoken: 16967543908123456
Complete example
Here's the complete working example that puts everything together.
- Flutter app development
- Non-Flutter platforms
import 'package:flutter/material.dart';
import 'package:pubnub/pubnub.dart';
void main() {
runApp(MaterialApp(home: PubNubApp()));
}
class PubNubApp extends StatefulWidget {
_PubNubAppState createState() => _PubNubAppState();
}
class _PubNubAppState extends State<PubNubApp> {
// PubNub instance
late PubNub pubnub;
show all 138 linesimport 'package:pubnub/pubnub.dart';
Future<void> main() async {
// Step 1: Initialize PubNub with configuration
final pubnub = PubNub(
defaultKeyset: Keyset(
subscribeKey: 'demo', // Replace with your subscribe key
publishKey: 'demo', // Replace with your publish key
userId: UserId('myUniqueUserId'),
),
);
// Step 2: Define the channel
final channel = "my_channel";
show all 45 linesRun the app
- Flutter app development
- Non-Flutter platforms
To run your Flutter application:
- Save the code to a file (e.g.,
main.dart
). - Make sure you have a Flutter project set up.
- Run the app with
flutter run
. - Enter messages and see them appear in the list.
To run your Dart application:
- Save the code to a file (e.g.,
pubnub_example.dart
). - Run it with
dart pubnub_example.dart
.
When you run the application, you should see output similar to the following:
Message to send: {text: Hello, world!, sender: dart_user}
Received message: {text: Hello, world!, sender: dart_user}
Message "Sent" with timetoken: 16967543908123456
Cleanup complete.
Congratulations! You've just subscribed to a channel and sent your first message with PubNub.
Troubleshooting
If you don't see the expected output, here are some common issues and how to fix them:
Issue | Possible Solutions |
---|---|
No connection message |
|
Message not received |
|
Build errors |
|
For more detailed troubleshooting information, refer to the Troubleshooting section of the SDK documentation.
Next steps
Great job! 🎉 You've successfully created your first PubNub application with Dart. Here are some exciting things you can explore next:
- Build chat
- Advanced features
- Real examples
- More help
- Build a full-featured chat application with user presence and typing indicators.
- Implement user Presence to show who's online.
- Add typing indicators and read receipts.
- Try out Presence to track online/offline status.
- Implement Message Persistence to store and retrieve messages.
- Use Access Manager to secure your channels.
- Explore our GitHub repository for more code.
- Check out the Flutter Simple Chat sample app.
- Check out our SDK reference documentation for detailed API information.
- Join our Discord community to connect with other developers.
- Visit our support portal for additional resources.
- Ask our AI assistant (the looking glass icon at the top of the page) for help.