Introduction to Diffusion Real-Time Messaging through a simple application using Diffusion Cloud.
A set of simple projects, illustrating production and consumption of messages to and from a Diffusion Cloud instance.
These JavaScript code examples enable users to interact in real-time across various rooms or topics. You can also use other programming languages from our SDKs, including iOS, Android, C, .NET, and more.
Lesson 1 code example introduces the concept of Publish and Subscribe to ‘Topics’ (data structures). In Diffusion, data is stored and distributed through Topics.
Lesson 2 code example introduces the concept of Time Series topic to hold a historical sequence of events. Time series topics are useful for collaborative applications such as chat rooms. Multiple users can concurrently update a time series topic.
Lesson 3 code example introduces the concept of Security by requiring sessions to authenticate and use role-based authorization to define the actions that a client can perform.
Make sure to add Diffusion library to your code. For JavaScript, we have added the following line in our public/chat.html
:
Set lines 44-46 of public/js/app.js
to the hostname of your Diffusion Cloud service, which you can find in your service dashboard. You can also leave the default values and connect to our sandbox service:
Really easy, just open the index.html file locally and off you go!
(Click to watch the video)
In Diffusion, data is stored and distributed through ‘Topics’ (data structures). Session can subscribe to a topic to receive notifications when the topic value changes and can also update the value. When a topic is updated, all its subscribers are notified of the new value. Diffusion takes care of efficiently broadcasting value changes, even if there are hundreds of thousands of subscribers.
diffusion.connect({
host : host, // Use your Diffusion service or connect to our sandbox "diffusionchatapp.eu.diffusion.cloud"
principal : "user",
credentials : "password"})
session.topics.add(_roomTopic, diffusion.topics.TopicType.JSON);
session.addStream(_roomTopic, diffusion.datatypes.json());
session.select(_roomTopic);
session.topicUpdate.set(_roomTopic, diffusion.datatypes.json(), { text: msg, name: name, timestamp: new Date().toLocaleTimeString() });
Introduce the concept of a Time Series topic to hold a sequence of events. Time series topics are useful for collaborative applications such as chat rooms. Multiple users can concurrently update a time series topic.
diffusion.topics.TopicSpecification : TopicType.TIME_SERIES
diffusion.topics.TopicSpecification(diffusion.topics.TopicType.TIME_SERIES,
{
TIME_SERIES_EVENT_VALUE_TYPE : "json",
TIME_SERIES_RETAINED_RANGE: "limit 100",
TIME_SERIES_SUBSCRIPTION_RANGE: "limit 100"
});
session.timeseries.append(_roomTopic,
{
text: msg
name: name,
timestamp: new Date().toLocaleTimeString()
},
diffusion.datatypes.json());
Introduce the concept of Security by requiring sessions to authenticate and use role-based authorization to define the actions that a client can perform.
session.security.authenticationScriptBuilder();
authenticationScriptBuilder
.addPrincipal(name, password, ['CLIENT','TOPIC_CONTROL'])
.build();
session.security.updateAuthenticationStore(addUserScript);
Now that you have a fully functional messaging app, try building a “moderator” app, to monitor the conversation for explicit/inappropriate messages. When specific text is identified in a conversation, the message value can be bleeped out, or access/permission to the room can be dynamically updated based on the user’s behavior.
Stay tuned for our next video tutorial introducing the basic concept of your Diffusion dashboard and console. We will update this blog with the link to the video in the next few days!