Context:
- I am currently utilizing the Pusher API.
- PHP serverside functionality is being employed.
- The Pusher package has been installed on the server side via composer.
Implementation Approach
I am in the process of creating a slideshow controlled by a single user. Whenever this user changes the slide, I trigger an AJAX call to update all users viewing the page with the new slide number.
$('#nextCard').click(function(){
if ($('#card-' + (cardId + 1)).length != 0) {
$('#card-' + (cardId)).hide();
cardId++;
$('#card-' + (cardId)).show();
location.hash = cardId;
/**
* make ajax call to push function
*/
$.ajax({
method: 'post',
url: '<?php echo base_url('learn/pusher'); ?>',
data: {card_id: cardId},
dataType: 'json',
async: false,
});
}
});
The updated slide number is sent through AJAX to the server and then relayed to users via Pusher in real time.
Pusher distributes the slide number simultaneously to all users on the same page, facilitating the seamless distribution of changes across multiple screens.
var channel = pusher.subscribe('notifications');
channel.bind('new-notification', function(data) {
// call appropriate function
});
After receiving the data pushed by Pusher, the corresponding function is executed to reflect the changes.
Inquiry
Initially, I presumed that Websockets was meant to replace technologies like AJAX. However, my current setup entails using AJAX to transmit Websockets data to Pusher from the server side.
I believed Websockets to be superior to AJAX due to its speed, but it seems that my reliance on AJAX may be hindering this aspect.
Am I employing Websockets correctly in this scenario?