Cyber Monday Sale! 50% off themes, plugins, graphics, audio + video! Cyber Monday Sale! Download Now
Advertisement
  1. Code
  2. Cloud Services
Code

Get Started With Pusher: Build a Chat App With Channels, PHP, and Vue.js

by
Difficulty:BeginnerLength:ShortLanguages:
This post is part of a series called Get Started With Pusher.
Get Started With Pusher: Demonstrating Real-Time Communication With Channels
Get Started With Pusher: Using Private Channels
Sponsored Content

This sponsored post features a product relevant to our readers while meeting our editorial guidelines for being objective and educational.

Channels from Pusher is a platform that allows you to give your apps seamless real-time data. 

In this post, I'll show you how to write the functional components of a very simple chat app. It's a stripped-down example, but you'll see how Channels can simplify the implementation of real-time communication in a web app.

 

Setting Up the Server

Our server application is a single PHP file called messages.php which will handle the POST requests coming from the browser. Our message handler will send the client’s messages to the Channels service, which will then broadcast those messages to other clients.

When using PHP for your server application, you want to download and use the Channels library, and you can install that library using composer and the following command:

The code for messages.php is almost an exact copy of what you can find on the Getting Started page in your Channels dashboard. There are just a few modifications.

First, you need to require the autoload.php file to use the Pusher library:

Next, the data coming from the client is in JSON format, so we obviously want to decode it into a workable PHP array.

We then want to set up our Pusher object so that we can then trigger an event.

My PHP installation does not work if the encrypted option is enabled, so I omitted it from my code. Your mileage may vary, but it's important to note that the encrypted option determines whether or not the data sent between the server and Channels is encrypted. It has nothing to do with the connection between Channels and your clients—the client library handles that.

The final line of our server's code sends the message data to Channels:

As with other server libraries, we pass three things to the trigger() method:

  1. The channel name: anon-chat
  2. The event name: send-message
  3. The payload: $data

Notice that the channel and event names are different than the channel and event names used on the Getting Started page. You do not have to create new channels or define custom events in the dashboard; just use whatever channel and event names you want in your code.

Completing the Client

Our client is a web page with three Vue.js components powering the UI. The main component is called ChannelsChat, and it is where we will put our Pusher object that listens for send-message events in the anon-chat channel. Let's use the created hook.

In this code, we create the Pusher object, subscribe to the anon-chat channel, and listen for send-message events.

Because this is a chat application, we need to store the message history so that whoever is using the application can see all the messages they received during their session. The easiest way to accomplish this is to store each message as an element in the array. So let's add a messages data property to this component, as shown in the following code:

Then, when we receive a message, we'll simply push() it to our array, as shown in the following code:

We'll then pass the messages to the MessageView component:

Sending Messages

The last of our code belongs in the MessageSend component; when the user types a message and clicks the Send button, we need to send that data to messages.php.

First, let's make sure the user typed something into the text box. Otherwise, there's no need to do anything!

The message property is bound to the <input/>'s value, so we'll use that to determine if we have any data.

Next, we send the POST request to message.php, and the data is an object with a message property.

If the request is successful, we clear the message property's value, which in turn clears the <input/>'s value (remember that they're bound). If the request fails, an alert box tells the user that an error occurred.

Screenshot of sample chat

That's it for the code. So open two browser windows and point them to index.php. Start sending messages and you should see both windows automatically update and display the messages.

Conclusion

As you can see, Channels makes it incredibly easy to quickly add real-time communication to your applications, and it didn't even require a lot of code! 

You also learned that you can create channels and events on the fly as you write your code. There's no need to set them up prior to using them. 

And finally, you learned how you can set up your applications to incorporate real-time communication. Simply handle incoming user input from your server, and trigger events based on that input.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.