PubNub Dart SDK 5.1.0
This page outlines the steps to follow to create a simple Hello World application with PubNub. This covers the basics of integrating PubNub in your application: setting up a connection to PubNub, and sending and receiving messages.
PubNub account
Sign in or create an account to create an app on the Admin Portal and get the keys to use in your application.
When you create a new app, the first set of keys is generated automatically, but a single app can have as many keysets as you like. We recommend that you create separate keysets for production and test environments.
Download the SDK
Download the SDK from any of the following sources:
Use pub
To integrate PubNub into your project using the pub package manager, add the following dependency in your pubspec.yml
file:
dependencies:
pubnub: 5.1.0
Then, run the dart pub get
command from the directory where your pubspec.yml
file is to install the required PubNub package.
Get Code: Source
https://github.com/pubnub/dart
Configure PubNub
In the IDE of your choice, create a new getting_started.dart
file with the following content. This is the minimum configuration you need to send and receive messages with PubNub.
Make sure to replace myPublishKey and mySubscribeKey with your app's publish and subscribe keys from the Admin Portal.
var pubnub = PubNub(
defaultKeyset:
Keyset(subscribeKey: 'mySubscribeKey', publishKey: 'myPublishKey', userId: UserId('myUniqueUserId')));
For more information, refer to the Configuration section of the SDK documentation.
Add 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.
// 'subscription' will be discussed later
subscription.messages.listen((message) {
print(message.content);
});
For more information, refer to the Subscription section of the SDK documentation.
Publish and subscribe
To receive messages sent to a particular channel, you subscribe to it. When you publish a message to a channel, PubNub delivers that message to everyone subscribed to that channel.
To subscribe, you send a subscribe()
call. It is best to define the channel and the message before you introduce the listeners and send the subscribe call, so make sure to place the relevant code in the appropriate places within your code.
var channel = "getting_started";
var subscription = pubnub.subscribe(channels: {channel});
for(var i=1; i<=5; i++) {
await pubnub.publish(channel, 'Message no. $i');
await Future.delayed(Duration(seconds: 1));
}
For more information, refer to the Publish and Subscribe section of the SDK documentation, and to Publishing a Message.
Putting it all together
Your getting_started.dart
file should now look similar to the following:
import 'package:pubnub/pubnub.dart';
void main() async {
// Create PubNub instance with default keyset.
var pubnub = PubNub(
defaultKeyset:
Keyset(subscribeKey: 'demo', publishKey: 'demo', userId: UserId('myUniqueUserId')));
// Subscribe to a channel
var channel = "getting_started";
var subscription = pubnub.subscribe(channels: {channel});
// Print every message
subscription.messages.listen((message) {
print(message.content);
show all 27 linesNow, run your app to see if you did everything correctly.
Save the getting_started.dart
file and run it with dart getting_started.dart
from the terminal. You can use the one that is built in your IDE or the one that your operating system provides. You should see output similar to the following:
Message no. 1
Message no. 2
Message no. 3
Message no. 4
Message no. 5
Congratulations! You've just subscribed to a channel and sent your first message.
Walkthrough
Instead of focusing on the order in which you wrote the code, let's focus on the order in which it runs. The app you just created does a few things:
- configures a PubNub connection
- subscribes to a channel
- adds the
message
event listener - publishes a message
- unsubscribes
Configuring PubNub
The following code is the minimum configuration you need to send and receive messages with PubNub. For more information, refer to the Configuration section of the SDK documentation.
var pubnub = PubNub(
defaultKeyset:
Keyset(subscribeKey: 'demo', publishKey: 'demo', userId: UserId('myUniqueUserId')));
Add 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.
You added a single message
listener to the app, which listens for incoming messages on a particular channel. When it receives a message, the app simply prints the received message. This is why you see "Hello, World" displayed in the console. The presence listener reacts to events connected to presence. In this example, the presence listener is a no-operation.
// Print every message
subscription.messages.listen((message) {
print(message.content);
});
For more information, refer to the Publish and Subscribe section of the SDK documentation.
Publishing and subscribing
PubNub uses the Publish/Subscribe model for realtime communication. This model involves two essential parts:
- Channels are transient paths over which your data is transmitted
- Messages contain the data you want to transmit to one or more recipients
When you want to receive messages sent to a particular channel, you subscribe to it. When you publish a message to a channel, PubNub delivers that message to everyone who is subscribed to that channel. In this example, you subscribe to a channel named myChannel
.
A message can be any type of JSON-serializable data (such as objects, arrays, integers, strings) that is smaller than 32 KiB. PubNub will, in most cases, deliver your message to its intended recipients in fewer than 100 ms regardless of their location. You can also share files up to 5MB.
// Send a message every second for 5 seconds
for(var i=1; i<=5; i++) {
await pubnub.publish(channel, 'Message no. $i');
await Future.delayed(Duration(seconds: 1));
}
You can subscribe to more than one channel with a single subscribe call but in this example, you subscribe to a single channel:
var channel = "getting_started";
var subscription = pubnub.subscribe(channels: {channel});
For more information, refer to the Publish and Subscribe section of the SDK documentation, and to Publishing a Message.
Next steps
You have just learned how to use the Dart SDK to send and receive messages using PubNub. Next, take a look at the SDK's reference documentation which covers PubNub API in more detail.