By: Category: Ruby on Rails,Web Development Technologies: Ruby on Rails
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
create-home-page-in-pusher
4) Create the View of Index:
create-view-index-in-pusher
5) generate a controller for creating and storing the post:
— rails generate controller Post create a store
6) Set the routes:
set-the-routes-in-pusher
7) Design the view of creating a new post:
creating-new-post
8) Add gem ‘cloudinary’ in gemfile and bundle install
9) write the logic to save the post:
save-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:
config-pusher
12) Include the Pusher script in the header of the application layout:
<script src=”https://js.pusher.com/4.1/pusher.min.js"></script>
13) Now, Update the index.html.erb:
update-index
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.