Real-Time Messaging for Meteor With Meteor Streams

This is 2013. If you are going to build a webapp, you must add real-time capabilities to the app. It is the standard. Meteor does a pretty good job at helping you to quickly build and make apps real-time. But meteor is tightly coupled with MongoDB and it is the only way to add real-time capabilities. Sometimes, this is overkill.

MongoDB is a perfect match for Meteor. But we don't need to use MongoDB for all our real-time activities. For some problems, messaging based solutions work really well. It's the same problem that pubnub and real-time.co are also addressing.

It would be great if we could have a hybrid approach to real-time, in Meteor, combining the MongoDB Collection based approach and a Messaging based approach. Thus Meteor Streams was born to add this messaging based, real-time communication to Meteor.


Introducing Meteor Streams

A Stream is the basic building block of Meteor Streams. It is a real-time EventEmitter. With a Stream, you can pass messages back and forth between connected clients. It is highly manageable and has a very good security model.

Lets Give It a Try

Let's create a very simple, browser console based chat application with Meteor Streams. We'll first create a new Meteor application:

Next we install Meteor Streams from the atmosphere:

Then we need to create a file named chat.js and place in the following code:

Start your app with:

Your app will now be running on - http://localhost:3000.

Now you have a fully functioning chat app. To start chatting, open the browser console and use the sendChat method as shown as below.

Browser Console based Chat with Meteor Streams

Let's Dive In Further

It's kind of hard to understand Meteor Streams with just a simple console based example, like the one we just built above. So, let's build a full featured chat application to become more familiar with Meteor Streams.

The App

The app we are creating is a web based chat application. Anyone can chat anonymously. Also, users can register and chat with their identity(username). It also has a filtering system, which filters out bad words (profanity).

At the end, it will look something like this. You can grab the source code from github to see the final result.

Meteor Streams Chat App in Action

Let's Create the App

Let's create a standard Meteor app and install Meteor Streams from atmosphere. We'll also be adding support for bootstrap and Meteor Accounts.

Let's Build the UI

The user interface for our app will be pretty simple. We have a div showing the chat messages and an input box to enter in new chat messages. See below for the complete HTML of our UI. Check out the inline comments if you need help understanding the code.

Add the following content into client/home.html:

Wiring Up Our Chat

Meteor's reactivity is an awesome concept and very useful. Now, Meteor Streams is not a reactive data source. But it can work well with local only collections to provide reactivity.

As the name implies, local only collections do not sync its data with the server. Its data is only available inside the client(browser tab).

Add the following content into lib/namespace.js to create our local only collection:

Now it's time to wire up our templates with the collection. Let's do following:

  • Assign the collection to the messages helper in the chatBox template.
  • Generate a value for the user helper in the chatMessage template.
  • When the Send Chat button is clicked, add the typed chat message into the collection.

Add the following content to client/ui.js:

With the above changes you'll be able to chat, but messages are only display on your client. So let's handover the rest of the job to Meteor Streams.

Let's Create the Stream

We'll be creating the stream on both the client and the server (with the same name) and adding the necessary permissions.

Append the following code into lib/namespace.js to create the stream:

Just creating the stream alone is not enough; we need to give the necessary permissions, which allow clients to communicate through it. There are two types of permissions (read and write). We need to consider the event, userId, and the subscriptionId when we are creating the permission.

  • userId is the userId of the client connected to the stream.
  • subscriptionId is the unique identifier created for each client connected to the stream.

For our chat app, we need to give anyone using the app full read and write access to the chat event. This way, clients can use it for sending and receiving chat messages.

Add the following code to server/permissions.js:

Connecting the Stream With the UI

Now that we have a fully functioning stream, let's connect it to the UI so others can see the messages that you are sending.

The first thing we need to do is add our chat messages to the stream, when we click on the Send Chat button. For that, we need to modify the code related to the Send Chat button's click event(click #send), as follows (in client/ui.js):

Then we need to listen to the stream for the chat event and add the message to the chatCollection which is being rendered in the UI, reactively. Append the following code to the client/ui.js file:

Now we need to modify the logic which generates the value for the user helper in the chatMessage template as follows:

  • Logged in user - user-<userId>
  • Anonymous user - anonymous-<subscriptionId>

Modify the code for the user helper in the chatMessage template to reflect the above changes (in client/ui.js):

Displaying the Username Instead of the userId

Showing just the userId is not very useful. So let's change it to display the actual username. Here, we'll be using Meteor Pub/Sub to get the username for a given userId.

First of all, lets configure Meteor Accounts to accept the username when creating the user. Add the following code to client/users.js:

Then let's create the publication for getting the user. Add the following code to server/users.js. It simply returns the username for a given userId.

Now we need to create a subscription on the client for each user we are interested in. We'll do this inside a method. Additionally, after we get the username, it needs to be assigned to a session variable. Then we can use the session variable inside the user helper to get the username reactively.

Append the following code into client/users.js:

Finally, let's modify the user helper in the chatMessage template to get the username from the session (in client/ui.js):


Filtering Out Bad Words

Our chat app will make sure to hide any profanity. If someone tries to send a message with some bad words, we need to filter those out. Meteor Stream has a feature called filters, which is designed for this. Let's see how we can filter out the word fool from any chat message.

Add the following code into server/filters.js:

Feel free to add in your own filters.

Our chat app is now complete. You can see a live version of the app at http://streams-chat.meteor.com. Additionally, the Source code for the app is available on Github.


Conclusion

In this tutorial we built a chat application using local only collections for adding in reactivity and used Meteor Pub/Sub for getting the username of a user. Hopefully you can see how nicely Meteor Streams can work with existing Meteor functionality. Still, this is just an introduction to Meteor Streams, for additional resources, check out the following links:

Tags:

Comments

Related Articles