Free up to 1MM monthly messages. No credit card required.
This post is Part One, and work your way through the entire tutorial. By the end, you’ll have a fully-functional AngularJS chat application powered by PubNub. You can navigate between parts with the links below, or the left sidebar.
In this section, we’ll focus on PubNub’s core features: publishing and subscribing for sending and receiving messages in real time.
The demo of the AngularJS chat app is available here
The source code is available in the github repository
To get started, you can either use a package manager like npm or Bower. Another option is to manually include a script tag with the library from the PubNub CDN.
Using the PubNub CDN:
npm install --save pubnub pubnub-angular
bower install --save pubnub pubnub-angular
Now, let’s create a simple chat room – each user is given a random identifier and can chat with other anonymous users.
In your Angular app, include this quick snippet to get started:
We’ve set up the PubNub app keys that you can get by signing onto the website, which you can then go create an app. We provided our own UUID to identify the user for the PubNub network. Don’t worry if you forget to set the UUID. The PubNub SDK will generate one for you.
To send messages, let’s add a
sendMessage() function in the Chat controller that will send the messages over the PubNub network.
We continue by adding a field and a button in our html template to trigger the
Now that the messages are sent out, let’s receive them. In order to do that, we need to subscribe to the
messages-channel channel and to react on the callback for when the message is sent and store it in the global messages array.
Now we can display the messages by iterating over them. It will be displayed on the page each time a new message is sent.
That’s it! You can find more information in the official PubNub AngularJS SDK documentation.
In the next tutorial, we will walk through how to improve our chat app for loading the previous messages when you initialize the app and when you scroll to the top of the screen. Also, we will walk through all of the best AngularJS practices to build a well-structured AngularJS app. Here’s how our chat app will look like:
See you in Part 2!
A roundtable discussion led by PubNub’s COO, Casey Clegg, exploring the topics of what it means to be human in a virtual world.
Dr. Joe Kvedar, Chair of the Board for the American Telemedicine Association, joins our COO, Casey Clegg, to discuss why...
Today, we are glad to announce that we are currently in the process of implementing ISO-27001 security standards.