By: Shivani Nerkar Category: Ruby on Rails 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 pusher for real-time data?
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="https://js.pusher.com/4.1/pusher.min.js"></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 with rails to get the real-time data without reloading the
page. Real-time functionality provided by Pusher to display updates in real-time.