Realtime web technologies make it really easy to add live content to previously static web pages.
Live content can bring a page alive, retain users and remove the need for them to refresh the page periodically. Realtime updates are generally achieved by connecting to a source of data, subscribing to the data you want to add to the page and then updating the page as the data arrives.
But why can’t this be achieved through a page to identify what data should be shown and where?
Well, with JQuery maybe it can!
In this tutorial we’ll create a jQuery plugin that makes it really easy to add realtime content to a page in 3 steps:
1. First of all, we’ll cover how to use a service called Pusher to subscribe to realtime data.
2. Then we’ll define a way of marking up an HTML5 document with ‘data-*’ attributes in a way which can then be queried by our realtime jQuery plugin and converted to realtime data subscriptions.
3. We’ll create the jQuery plugin which will use the attributes to subscribe to data and instantly display updates within the page.
How Pusher works
Pusher is a hosted service that makes it easy to add realtime content and interactive experiences to web and mobile apps.
In this article we’re going to show how simply connect, subscribe to some data and then update a page when the data comes in.
To demonstrate this we have to create a file called ‘example.html’ and include the Pusher JavaScript library from the Pusher CDN.
We know we’re going to use jQuery 2.0.0 so we should also include that now:
Creating a realtime jQuery plugin
How to Connect
Create an additional ‘