ITS
Laravel development
Introduction
Pusher is a hosted service that enables to use real-time data in web and mobile applications. Laravel Pusher has many features that make it a popular choice for building real-time applications. Here are some of the key features of Pusher:
1. Go to the Pusher website at Pusher.
2. Click on the “Sign up” button in the top right corner of the page.
3. Fill out the registration form with your name, email address, and password.
4. Click on the “Create account” button.
5. You will receive a confirmation email from Pusher. Click on the link in the email to verify your email address and activate your account.
6. Once your account is activated, you can log in to the Pusher dashboard and start using Pusher’s real-time communication infrastructure to build your applications.
1. Log in to your Pusher account and go to the dashboard.
2. Click on the “Create new app” button.
3. Fill out the form with your app’s name, description, and the technology you’ll be using (e.g., JavaScript, React, iOS, etc.).
4. Select the cluster you want to use for your app (Pusher provides different clusters located in different regions to optimize performance for your users).
5. Click on the “Create app” button to create your app.
Once you have created your app, Pusher will provide you with an App ID, API Key, and API Secret. You will need these credentials to use chat Pusher APIs and libraries in your application. You can find these credentials in the app dashboard under the “App Keys” tab.
After creating your app, you can configure the settings and features you want to use in your application. For example, you can create channels, configure webhooks, set up authentication, and more. Pusher’s documentation provides detailed guides and examples on how to use Pusher’s features in your application.
Configure app credentials to your .env file
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=
Don’t forget to clear cache : php artisan config:clear
Route::post(‘pusher/send-message’, ‘pusherController@sendMessage’)->name(‘pusher.sendmessage’);
Jquery:
var message = ‘Hello, This is my first real time message’;
$.ajax({
type: ‘POST’,
cache: false,
dataType: ‘json’,
url: ‘{{route(“pusher.sendmessage”)}}’,
contentType: false,
processData: false,
data: {message : message},
headers: {
‘X-CSRF-TOKEN’: $(‘meta[name=”csrf-token”]’).attr(‘content’)
},
success: function (result) {
if(result.response_code == 1){
alert(“Message has been sent”);
}else{
alert(“Fail to send message”);
}
},
error: function(){
alert(“Something went wrong please try again later”);
}
});
use Pusher\Pusher;
use Validator;
class pusherController extends Controller{
public function sendMessage(Request $request) {
$return_data[‘response_code’] = 0;
$return_data[‘message’] = ‘Something went wrong, Please try again later.’;
$rules = [‘message’ => ‘required’];
$messages = [‘message.required’ => ‘Please enter message to communicate.’];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
$message = implode(“
“, $validator->messages()->all());
$return_data[‘message’] = $message;
return $return_data;
}
try {
$options = [
‘cluster’ => env(‘PUSHER_APP_CLUSTER’),
‘useTLS’ => true
];
$pusher = new Pusher(
env(‘PUSHER_APP_KEY’),
env(‘PUSHER_APP_SECRET’),
env(‘PUSHER_APP_ID’),
$options
);
$response = $pusher->trigger(‘my-chat-channel’, ‘my-new-message-event’, [‘message’ => $request->message]);
if($response){
$return_data[‘response_code’] = 1;
$return_data[‘message’] = ‘Success.’;
}
} catch (\Exception $e) {
$return_data[‘message’] = $e->getMessage();
}
return $return_data;
}
}
/**
* @OA\Post(
* path=”/api/login”,
* summary=”Authenticate user and generate JWT token”,
* @OA\Parameter(
* name=”email”,
* in=”query”,
* description=”User’s email”,
* required=true,
* @OA\Schema(type=”string”)
* ),
* @OA\Parameter(
* name=”password”,
* in=”query”,
* description=”User’s password”,
* required=true,
* @OA\Schema(type=”string”)
* ),
* @OA\Response(response=”200″, description=”Login successful”),
* @OA\Response(response=”401″, description=”Invalid credentials”)
* )
*/
public function login(Request $request)
{
$credentials = $request->only(’email’, ‘password’);
if (Auth::attempt($credentials)) {
$token = Auth::user()->createToken(‘api_token’)->plainTextToken;
return response()->json([‘token’ => $token], 200);
}
return response()->json([‘error’ => ‘Invalid credentials’], 401);
}