create account

How to Create Realtime Notifications Using Pusher in Php by alfarisi94

View this thread on: hive.blogpeakd.comecency.com
· @alfarisi94 · (edited)
$25.68
How to Create Realtime Notifications Using Pusher in Php
![Screenshot_14.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1520271722/lyeb1yjb56co5nvgzfi3.png)
This tutorial I will share how to give realtime notification in your application by using php programming language, and surely we need a third party library, namely pusher. so just start our tutorial this time .

#### What Will I Learn?
- Use Pusher , Register and Setting .
- Use Pusher in Php to Handle Backend (Add Event and Channel) and Configuration settings.
- Use Pusher in Javascript to Handle Frontend (Use event and Connect to Channel).

#### Requirements
- Internet Connection
- Php >= 5.6
- Install Composer
- Install Pusher
- Localhost

#### Difficulty
- Intermediate


### Use Pusher
Before we actually make our realtime application notification. you must have a pusher account first, so you can do some setting in it. After you login to pusher, you can choose <b>"create my app"</b>. 
You can named the project, here I give the name <b>"notification"</b> .  And you can also select the <b>cluster</b> closest to your location . You can also choose what technology you will use in the frontend and backend, I use javascript in frontend, and php in the backend .


![Screenshot_6.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1520261456/vgwrn3jtdbtrdieyrhsc.png)

After you create new app. you will be instructed to install the composer and import the javascript pusher library .

![Screenshot_7.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1520261805/h4tr7evef3nfpqpb1gdd.png)

We can open the command prompt and do the composer install on your project .
Composer install

<code>composer require pusher/pusher-php-server</code>

![Screenshot_8.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1520262061/dkf38qbnk5xe9ngwvbyy.png)

After the installl is complete then you will see the vendor folder and <b>composer.json</b> has been made.

### Create Pusher Application on the Serverside
We will configure the server section, which will use the Php programming language .
I will create a new folder under the folder pusher, and that folder I will name the notification and in the folder I create a file with the name index.php. <b>"Pusher ->notification->index.php"</b>

<b>index.php</b>
<pre>
<code>
<?php
require('../vendor/autoload.php'); // load all functions in vendor
//Define Key pusher
define('APP_KEY','87cca8e5e7ea3ac15812');
define('APP_SECRET','73c94da408ce6bcb8ad1');
define('APP_ID','486604');
//make object of pusher
$pusher = new Pusher(
	APP_KEY,
	APP_SECRET,
	APP_ID,
	[
		'cluster' 	=>'ap1',
		'encrypted'	=> true
	]
);
$data['message'] = "Hello World , I'm Realtime Notification";
$pusher->trigger('my-channel','my-event',$data);
</code>
</pre>

<li><b>Import Vendor</b></li>
Because we are using third-party libraries, we have to import all function functions in the vendor folder with <b> autoload.php </b> <br>
<code>require('../vendor/autoload.php');</code>

<br>
<li><b>Initialization of the pusher object in a variable</b></li>
We will create a pusher object that we initialize in suati variable, create new object in pusher with <b>new pusher ()</b>. In that object we have some key which I will explain .
<pre>
<code>
$pusher = new Pusher(
	APP_KEY,
	APP_SECRET,
	APP_ID,
	[
		'cluster' 	=>'ap1',
		'encrypted'	=> true
	]
);
</code>
</pre>
<li><code>APP_KEY</code></li> : Contains the key of our application that we will use in the client as a link between the server and client<br>
<li><code>APP_SECRET</code></li> : APP_SECRET will only be used on the server side, not on the client side. <br>
<li><code>APP_ID</code></li> : This is the app_id we have on the pusher application we created earlier.<br>
<li><code>['cluster' 	=>'ap1', 'encrypted'	=> true]</code></li>: These are options. in the form of arrays. <b>'cluster'</b> and encrypted.

<br>
<li><b>Define Key</b></li>
<pre>
<code>
define('APP_KEY','87cca8e5e7ea3ac15812');
define('APP_SECRET','73c94da408ce6bcb8ad1');
define('APP_ID','486604');
</code>
</pre>
Where do we get the key? we can see it in our dashboar pusher, in tab <b>"app keys"</b> .

![Screenshot_9.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1520265375/epzikethgb5ucnfrsso3.png)

<li><b>Add Channel</b></li>
I will create a notification function, I will run with the method of <b>$pusher->trigger()</b>.
<pre>
<code>
$data['message'] = "Hello World , I'm Realtime Notification";
$pusher->trigger('my-channel','my-event',$data);
</code>
</pre>
in the trigger method , We pass three parameters, 
1 .<b>'my-channel'</b>  is thename of chanel.
2. <b>'my-event'</b> is the name of event.
3. <b>$data</b> is the message we will show there is the client side.


### Use Pusher in Clientside 
in the client side we will create a simple html file with the name <b>index.html</b> under the folder pusher. <b>pusher->index.html</b>

<b>index.html</b>
``` html
<!DOCTYPE html>
<html>
<head>
	<title>Tutorial Pusher</title>
	<script src="https://js.pusher.com/4.1/pusher.min.js"></script>
</head>
<body>
	<script type="text/javascript">
		Pusher.logToConsole = true;
		var pusher = new Pusher('87cca8e5e7ea3ac15812',{
			cluster 	:'ap1',
			encrypted	: true
		});
		var channel = pusher.subscribe('my-channel');
		channel.bind('my-event', function(data){
			alert(data.message)
		});
	</script>
</body>
</html>
```
<br>

<li><b>Import Library Pusher</b></li>
We need to import the library pusher in the client.  We can see the import script in dashboard pusher section in <b>get started</b> tab .

![Screenshot_10.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1520267612/c4iq82wwtfedfluiomrd.png)

<code>< script src="https://js.pusher.com/4.1/pusher.min.js" >< /script ></code>

<li><b>initialization of pusher in client</b></li>
Not just initialization on the server, We also need to initialize in the client, but the difference we just need it the <b>APP_KEY : 87cca8e5e7ea3ac15812</b>
<pre>
<code>
var pusher = new Pusher('87cca8e5e7ea3ac15812',{
			cluster 	:'ap1',
			encrypted	: true
		});
</code>
</pre>

<li><b>Subscribe Channel</b></li>
We will subscribe to the <b>subscribe()</b> method from pusher. and initialize in one variable <b>var channel</b>.
<br>
<code>var channel = pusher.subscribe('my-channel');</code> : <b>'my-channel'</b> is the name of the channel we define in the server side. So if we have some different applications during dy run this script, dy will automatically subscribe this channel.
<br>

<li><b>Binding Event</b></li>
Here we will user  <b>bind()</b> method from pusher to bind or we combine with the event we named in its serveside as <b>'my-event'</b>.
<code>channel.bind('my-event', function(data){}</code> : We receive the callback in the data. and i will <b>alert(data.message)</b> to see what's inside the data . 


To see the results I will run two browser tabs that one will run on the server and another to see the results of the notification . run the server on your localhost .

<b>Browser Server</b>

![Screenshot_11.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1520270863/ki8igab9ga4rgyg3mdhh.png)

in the server browser we just do reload, make sure every time reload, index.php managed to send notif.

<b>Browser Clientside</b>

in the client, we can see the alert that we put on the client side, we can see it by opening our <b>index.html</b> file in the browser.

![Screenshot_12.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1520271121/x1bekypw0giv2gxvvnbo.png)


If we change again in chanel message section, then we will get new message. I change the key of <b>$data['message']</b>in the index.php section. <code>$data['message'] = "I have been changed in the server side";</code> , the result will be like this.

![Screenshot_13.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1520271387/ptcolmzgepl1rs89pv2d.png)


Even i also get the same notif though i open a different browser as an example i will use two browser, chrome and mozilla.

![Screenshot_14.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1520271838/of4wqbbgtgczffbmmbzs.png)

Finally we have completed this tutorial, we have made realtime notification that we often see in social media, hopefully from this tutorial can give you idea or picture about realtime notification, enough of me thank you already follow this tutorial

<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@alfarisi94/how-to-create-realtime-notifications-using-pusher-in-php">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 57 others
properties (23)
authoralfarisi94
permlinkhow-to-create-realtime-notifications-using-pusher-in-php
categoryutopian-io
json_metadata"{"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"id":1903522,"name":"php-src","full_name":"php/php-src","html_url":"https://github.com/php/php-src","fork":false,"owner":{"login":"php"}},"pullRequests":[],"platform":"github","type":"tutorials","tags":["utopian-io","php","pusher","backend","web"],"users":["alfarisi94"],"links":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1520271722/lyeb1yjb56co5nvgzfi3.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1520261456/vgwrn3jtdbtrdieyrhsc.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1520261805/h4tr7evef3nfpqpb1gdd.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1520262061/dkf38qbnk5xe9ngwvbyy.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1520265375/epzikethgb5ucnfrsso3.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1520267612/c4iq82wwtfedfluiomrd.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1520270863/ki8igab9ga4rgyg3mdhh.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1520271121/x1bekypw0giv2gxvvnbo.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1520271387/ptcolmzgepl1rs89pv2d.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1520271838/of4wqbbgtgczffbmmbzs.png"],"image":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1520271722/lyeb1yjb56co5nvgzfi3.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1520261456/vgwrn3jtdbtrdieyrhsc.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1520261805/h4tr7evef3nfpqpb1gdd.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1520262061/dkf38qbnk5xe9ngwvbyy.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1520265375/epzikethgb5ucnfrsso3.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1520267612/c4iq82wwtfedfluiomrd.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1520270863/ki8igab9ga4rgyg3mdhh.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1520271121/x1bekypw0giv2gxvvnbo.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1520271387/ptcolmzgepl1rs89pv2d.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1520271838/of4wqbbgtgczffbmmbzs.png"],"moderator":{"account":"flauwy","time":"2018-03-06T02:38:25.812Z","reviewed":true,"pending":false,"flagged":false},"questions":[{"question":"Is the project description formal?","answers":[{"value":"Yes it’s straight to the point","selected":true,"score":10},{"value":"Need more description ","selected":false,"score":5},{"value":"Not too descriptive","selected":false,"score":0}],"selected":0},{"question":"Is the language / grammar correct?","answers":[{"value":"Yes","selected":false,"score":20},{"value":"A few mistakes","selected":true,"score":10},{"value":"It's pretty bad","selected":false,"score":0}],"selected":1},{"question":"Was the template followed?","answers":[{"value":"Yes","selected":true,"score":10},{"value":"Partially","selected":false,"score":5},{"value":"No","selected":false,"score":0}],"selected":0},{"question":"Is there information about the additional frameworks?","answers":[{"value":"Yes, everything is explained","selected":true,"score":5},{"value":"Yes, but not enough","selected":false,"score":3},{"value":"No details at all","selected":false,"score":0}],"selected":0},{"question":"Is there code in the tutorial?","answers":[{"value":"Yes, and it’s well explained","selected":true,"score":5},{"value":"Yes, but no explanation","selected":false,"score":3},{"value":"No","selected":false,"score":0}],"selected":0},{"question":"Is the tutorial explains technical aspects well enough?","answers":[{"value":"Yes, it teaches how and why about technical aspects","selected":true,"score":5},{"value":"Yes, but it’s not good/enough","selected":false,"score":3},{"value":"No, it explains poorly","selected":false,"score":0}],"selected":0},{"question":"Is the tutorial general and dense enough?","answers":[{"value":"Yes, it’s general and dense","selected":true,"score":5},{"value":"Kinda, it might be more generalized","selected":false,"score":3},{"value":"No, it’s sliced unnecessarily to keep part number high","selected":false,"score":0}],"selected":0},{"question":"Is there an outline for the tutorial content at the beginning of the post","answers":[{"value":"Yes, there is a well prepared outline in “What will I learn?” or another outline section","selected":false,"score":5},{"value":"Yes, but there is no proper listing for every step of the tutorial or it’s not detailed enough","selected":true,"score":3},{"value":"No, there is no outline for the steps.","selected":false,"score":0}],"selected":1},{"question":"Is the visual content of good quality?","answers":[{"value":"Yes","selected":true,"score":5},{"value":"Yes, but bad quality","selected":false,"score":3},{"value":"No","selected":false,"score":0}],"selected":0},{"question":"Is this a tutorial series?","answers":[{"value":"Yes","selected":true,"score":5},{"value":"Yes, but first part","selected":false,"score":3},{"value":"No","selected":false,"score":0}],"selected":0},{"question":"Is the tutorial post structured?","answers":[{"value":"Yes","selected":true,"score":5},{"value":"Not so good","selected":false,"score":3},{"value":"No","selected":false,"score":0}],"selected":0}],"score":74}"
created2018-03-05 17:46:54
last_update2018-03-06 02:38:27
depth0
children2
last_payout2018-03-12 17:46:54
cashout_time1969-12-31 23:59:59
total_payout_value17.886 HBD
curator_payout_value7.792 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length8,497
author_reputation5,678,893,550,406
root_title"How to Create Realtime Notifications Using Pusher in Php"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id42,430,554
net_rshares7,780,141,657,579
author_curate_reward""
vote details (121)
@flauwy ·
Thank you for the contribution. It has been approved.

You can contact us on [Discord](https://discord.gg/uTyJkNm).
**[[utopian-moderator]](https://utopian.io/moderators)**
properties (22)
authorflauwy
permlinkre-alfarisi94-how-to-create-realtime-notifications-using-pusher-in-php-20180306t023826123z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-03-06 02:38:39
last_update2018-03-06 02:38:39
depth1
children0
last_payout2018-03-13 02:38:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length172
author_reputation296,259,911,900,510
root_title"How to Create Realtime Notifications Using Pusher in Php"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id42,516,024
net_rshares0
@utopian-io ·
### Hey @alfarisi94 I am @utopian-io. I have just upvoted you!
#### Achievements
- WOW WOW WOW People loved what you did here. GREAT JOB!
- You have less than 500 followers. Just gave you a gift to help you succeed!
- Seems like you contribute quite often. AMAZING!
#### Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. <a href="https://discord.gg/zTrEMqB">Participate on Discord</a>. Lets GROW TOGETHER!
- <a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for my Witness With SteemConnect</a>
- <a href="https://v2.steemconnect.com/sign/account-witness-proxy?proxy=utopian-io&approve=1">Proxy vote to Utopian Witness with SteemConnect</a>
- Or vote/proxy on <a href="https://steemit.com/~witnesses">Steemit Witnesses</a>

[![mooncryption-utopian-witness-gif](https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif)](https://steemit.com/~witnesses)

**Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x**
properties (22)
authorutopian-io
permlinkre-alfarisi94-how-to-create-realtime-notifications-using-pusher-in-php-20180306t170721252z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-03-06 17:07:21
last_update2018-03-06 17:07:21
depth1
children0
last_payout2018-03-13 17:07:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,143
author_reputation152,955,367,999,756
root_title"How to Create Realtime Notifications Using Pusher in Php"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id42,669,210
net_rshares0