php Send Real Time Notification to Clients Through Pusher using PHP

php Send Real Time Notification to Clients Through Pusher using PHP

In this post we will show you php Send Real Time Notification to Clients Through Pusher in PHP, Now and then we have to send ongoing notices to our customers. Be that as it may, executing it through ajax is to a great degree not suggested as regular Ajax ask for can back off your server and in the back case can cause Slowloris assault in your apache server since apache server makes strings for each demand. So the best and the right approach to actualize the constant framework is through web attachment associations in this manner the association is constantly settled amongst customers and server without the need to over-burden server through ceaseless solicitations. In this instructional exercise, we will demonstrate to send the free ongoing warnings to continuous customers through Pusher in PHP.

For php Send Real Time Notification to Clients Through Pusher in PHP, we begin coding we have to make an application in Pusher and need to get application key and mystery. The application creation is simple and straight forward process. All you have to do is recently join with your Google or GitHub record to Pusher by going by pusher.com. Their essential designs are totally free.

Presently to make standard bootstrap cautions into “snarl” like notices we am utilizing a free module called Bootstrap Notify. You can likewise download it from http://bootstrap-notify.remabledesigns.com/.

We should Start Coding for php Send Real Time Notification to Clients Through Pusher using PHP

We have to make two pages one for the customers to indicate warnings and one for the server to send notices to customers. In customer page, we utilize some javascript codes to set up an association with Pusher through an attachment based association. The javascript codes in the customer’s pages which we have to indicate constant notice are given underneath.

<script src="https://js.pusher.com/4.0/pusher.min.js"></script>
<script>
// for php Send Real Time Notification to Clients Through Pusher using PHP
// Enable pusher logging - don't include this in production
Pusher.logToConsole = true;

var pusher = new Pusher('your app key', {
cluster: 'ap2',
encrypted: true
});

var channel = pusher.subscribe('my-channel');
channel.bind('my-event', function(data) {
console.log(data.message);

});

</script>

Presently in server side, we will send our warnings to every associated customer through pusher. The server side code for sending the notice to customers is given underneath.

// set php Send Real Time Notification to Clients Through Pusher using PHP
// autoload require
require('vendor/autoload.php');

$options = array(
'cluster' => 'ap2',
'encrypted' => true
);
$pusher = new Pusher(
'add your app key', // add app key
'add your app secret', // add app secret
'add your app id', // add app id
$options // add options
);

$data['message'] = 'Hello onliecode for php Send Real Time Notification';
$pusher->trigger('my-channel', 'my-event', $data);

Note for php Send Real Time Notification to Clients Through Pusher using PHP

You have to download Pusher library for PHP by utilizing arranger before doing server side coding. On the off chance that you are not acquainted with the arranger, It is only a Dependency Manager for PHP. You can undoubtedly download it from https://getcomposer.org/. Subsequent to introducing it explore to your venture organizer through terminal or cmd and glue the order given underneath. A seller organizer is made inside your venture envelope which contains every one of your libraries. Presently require the autoload.php document inside merchant organizer in all your server side pages.

author require pusher/pusher-php-server

Now Let’s Create A Demo App for php Send Real Time Notification

In this demo application we will acknowledge a name from the customer side content box and send it to the server through jquery ajax post capacity and after that the customer will send a push warning to every single associated customer through pusher.

Index.html

The entire code for an index.html page of our demo application is given underneath.

<!DOCTYPE html>
<head>
<title>php Send Real Time Notification - onlinecode</title>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- add Bootstrap 3 -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<!-- add Bootstrap Notify - Optional -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-notify/0.2.0/css/bootstrap-notify.min.css" >
<script src="https://shareurcodes.com/js/bootstrap-notify.min.js" ></script>

<!-- add Material Design Lite Custom - Optional -->
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-deep_purple.min.css" />

<!-- add Pusher -->
<script src="https://js.pusher.com/4.0/pusher.min.js"></script>
<script>

// add hear Enable pusher logging - don't include this in production
Pusher.logToConsole = true;
// pusher
var pusher = new Pusher('add your app key', {
cluster: 'ap2',
encrypted: true
});
// for channel pusher
var channel = pusher.subscribe('my-channel');
channel.bind('my-event', function(data) {
console.log(data.message);
$.notify(data.message, {
newest_on_top: true
});
});

</script>

<style type="text/css">
.pagination>.active>a, .pagination>.active>a:focus, .pagination>.active>a:hover, .pagination>.active>span, .pagination>.active>span:focus, .pagination>.active>span:hover {
background-color: rgb(124,77,255);
}
[data-notify="container"] {
background-color: rgb(240, 240, 241);
border-color: rgba(148, 148, 148, 0.3);
border-radius: 6px;
right : 100px !important;
color: rgb(124,78,254);
font-size : 19px;
font-weight : bolder;
}
</style>
<!-- end style -->
</head>
<!-- end head -->
<body>
<div class="container">
<p>&nbsp;</p>
<p>&nbsp;</p>
<a><h3 class="text-center"> php Send Real Time Notification to Clients Through Pusher - onlinecode</h3></a>

<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="well">
<div class="panel panel-default">
<div class="panel-body">
<div class="row">
<div class="col-md-10 col-md-offset-2">
<form class="form-inline" id="form"  method="post" >
<div class="form-group">
<label for="fromdate">&emsp;Enter Your Name : &emsp;</label>
<input type="text"  class="form-control"  name="name" placeholder="Enter Your Name Hear" required>
</div>
<div class="form-group">
&emsp;&emsp;&emsp;<button style="background-color: rgb(124,77,255);color: #fff" type="submit" id="search"  class="btn btn-default">SUBMIT</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div> <!-- end div -->
<script >
$(document).ready(function(){
$("#form").on("submit", function(event){
event.preventDefault();
$.post('server.php',$("#form").serialize());
});
});
</script> <!-- end script -->
</body> <!-- end body -->
</html> <!-- end html -->

Note: we utilized some custom CSS only to style the application. It is totally discretionary for your situation. Presently we utilized bootstrap tell module to show the notices. You can utilize a ready box or anything you like. The jquery ajax present capacity is utilized on pass the name to the server side for php Send Real Time Notification.

server.php

The total code for server.php record is given beneath.

<?php
If( $_SERVER['REQUEST_METHOD']  != 'POST'  ){
die();
}
require('vendor/autoload.php');
// add value
$options = array(
'cluster' => 'ap2',
'encrypted' => true
);
$pusher = new Pusher(
'add app key',
'add app secret',
'add app id',
$options // your options
);

if(isset($_POST['name']))
{
$name = $_POST['name']
}
else
{
$name = "Client";
}

$data['message'] = 'Hello '.$name;
$pusher->trigger('my-channel', 'my-event', $data);

The server side sends push notice to all customers which are associated with pusher through web attachment.

Now we should see the yield picture of demo app

php Send Real Time Notification to Clients Through Pusher – onlinecode.

Demo for php Send Real Time Notification

You can demo above program by going to following connection.

Note: Open this URL in different programs to ensure that your name is communicated to various customers constant. In the event that you see whatever other names when you open this page is on the grounds that some different clients are trying this demo application. we am additionally not sparing any of your names in server side so you can test it securely.

On the off chance that anyone has any proposals or questions or need any assistance remark beneath and we attempt will react to each one of you as right on time as could be expected under the circumstances. More instructional exercises like making constant WebSockets talk application and Facebook visit bot is coming soon. So please stay tuned and leave your recommendations underneath.

Leave a Comment

Your email address will not be published. Required fields are marked *

2  +  2  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US