Guide: How to use Node.js to display “Facebook Likes”

Node.js can be used to create event-based programming, and this post provides a practical script to demonstrate its benefits.

A script is being developed to display the number of “Facebook Likes” for a given page, with an additional feature that refreshes the count every two seconds.

Get started styling your page with CSS to make the number of Likes, 2630405, stand out.

To give you an idea

Before using Node.js, it’s important to consider the server overhead that can come with making AJAX calls, such as those used to find the number of likes in a given time period. PHP and other server-side programming languages are commonly used for this purpose, but they can be inefficient.

Five users visiting the same page can result in 10 accesses to graph.facebook.com in two seconds, as each user updates their number of likes every two seconds, running as a separate thread. This is a time-consuming I/O operation.

No extra server is needed to access Facebook data - one connection to the Facebook server is all that’s required, and the time it takes to get and process the result (like count) can be drastically reduced.

What steps are needed to put this plan into action? We’ll explore that in the following sections.

To work

To get started with Node.js, you must have it installed and running on a web hosting account that supports v8 environments. For instructions on how to do this, refer to the “Getting Started with Node.js” and “Installing Node.js” topics in our Beginners Guide to Node.js article.

A server is being used to update the number of likes on Facebook every two seconds. This process, referred to as “ACTION 1,” involves a page that refreshes itself automatically using AJAX.

When multiple users visit the same page, an event listener is added to the server for each AJAX request they make. When “ACTION1” is completed, these listeners are triggered.

Examining the code of the server is worth exploring.

A new Facebook client was created using the my_http library, and an event emitter was initialized. A request was then sent to the Facebook Graph API for data, with a response listener set up to capture the data.

A server running on port 8080 has been set up to retrieve data from Facebook and emit it as a string. Every second, the server will make a request to Facebook and parse the response into JSON format. The data is then emitted as a string for use by the server.

A new Facebook client has been created, allowing users to access the social media platform’s Graph API. The client is powered by an EventEmitter, which will enable users to receive notifications when certain events occur.

A custom HTTP client was created to access Facebook Graph API’s facebook_client, with the EventEmitter () function triggered upon completion of “ACTION1”.

The code below reveals the answer.

A Facebook client is making a request to the Graph API for data, and listening for a response. When the response is received, it will add any data to a body string.

The function sends a request to Facebook and emits a “data” event with the number of likes when it receives a response.

The get_data function retrieves data from the Facebook API by making a GET request with the request method.

A client has requested a ‘GET’ from a host URL using the get_url.

To access the details of a Facebook page, we must make a request to http://graph.facebook.com/19292868552 and add three listeners.

At the end of “ACTION1,” the listener associated with the event_emitter object must be activated. The facebook_emitter.emit method is used to explicitly trigger it.

Facebook Platform, founded in May 2007, has 2738595 likes and is a product/service. Its mission is to make the web more open and social. It enables anyone to build social apps on Facebook and the web.

To get the number of likes on a Facebook page, convert the “likes” object of the data object to a string and pass it to the broadcast function.

The request will be terminated following this action.

A web server has been set up to listen on port 8080 and retrieve data from Facebook every second. When a request is made for the data, the server will respond with the requested information in plain text format.

A small change has been made to the server creation process outlined in a previous tutorial: the load_file function is used to serve static files for all URLs except /getdata.

To access data from Facebook, an AJAX request must be made to the URL http://localhost:8080/getdata. An event listener should be added to facebook_emitter with each request, similar to addListener but with the listener killed after broadcast to avoid memory leaks. To watch it, replace once with addListener. The get_data function is also called every second using setInterval.

The output will be displayed on an HTML page created by us.

The jQuery AJAX part is straightforward: a call to the load_content function runs an infinite loop, updating the number of likes in real time.

AJAX requests take an average of 1 second to process, resulting in a delay that leaves the request incomplete for that time.

Have a question about delaying an AJAX response from the server to get the number of Facebook likes? Leave it in the comment section and we’ll help you out. Thanks!

Geo Paul is an experienced web and iPhone developer, with four years of experience in PHP and two years in iPhone app development. He specializes in working with PHP, Codeigniter, WordPress, jQuery and Ajax.

How to use Node.js to display “Facebook Likes”: benefits

Faq

Final note

This guide provides step-by-step instructions on how to use Node.js to display “Facebook Likes.” If you have any questions, please reach out for help. Show your support by sharing this article with your friends.

If you have any questions about using Node.js to display “Facebook Likes”, please ask us in the comments section or contact us directly. This guide is for educational purposes only. If you want to correct any misinformation, add more methods, or need help, please use our contact page. You can also follow us on Whatsapp, Facebook and Twitter for questions and we will respond within 24-48 hours (weekends excluded). For the latest software updates and discussion about any software join our Telegram channel.