How To Fetch Realtime Data Using Pusher

How To Fetch Realtime Data Using Pusher

19 Sep 2022
How To Fetch Realtime Data Using Pusher

What is a pusher?

Pusher is a hosted API service that makes adding real-time data and functionality to web and mobile applications seamless. Pusher works as a real-time communication layer between the server and the client.

How to use a pusher for real-time data?

Following are the steps:

1) Create the rails application
- rails new real_time_app

2) Create a model and migrate that
- rails generate model Post title:text body:text author:text media:text
- rails db:migrate


3) Create a home page
- rails generate controller Home index
- create a home controller


4) Create the View of Index:


5) generate a controller for creating and storing the post:
— rails generate controller Post create a store

6) Set the routes:


7) Design the view of creating a new post:


8) Add gem ‘cloudinary’ in gemfile and bundle install

9) write the logic to save the post:


10) Add the real-time functionality with pusher:
— add gem ‘pusher’ in gemfile and run bundle install
- create an account for the pusher and get all details that we required in the pusher.rb file


11) Create a config/initializers/pusher.rb file and add the following content to it:


12) Include the Pusher script in the header of the application layout:
<script src=”"></script>

13) Now, Update the index.html.erb:


14) Start the server and now you can see the real-time post.

Conclusion: We can use a pusher to get real-time data without reloading the rails to the page. Real-time functionality provided by Pusher to display updates in real time.

Leave a comment: