If you are already using Realtime.co to implement Pub/Sub or Presence in your applications, but are looking to migrate to Ably, here's a quick guide on how you can implement the various Realtime.co features in Ably:


1. Connection:

  •      Using Realtime.co, you'll need to create a client, set a cluster URL and then have your client connect to your application, as shown below:
ortcClient = RealtimeMessaging.createClient();
ortcClient.setClusterUrl('https://ortc-developers.realtime.co/server/ssl/2.1/');
ortcClient.connect('INSERT_YOUR_APP_KEY_HERE', 'testToken');


  •     Using Ably, you can simply connect to the realtime library using your Ably API key if you wish to use Basic Authentication. 
var realtime = new Ably.Realtime({ key: apiKey });

Note that you can also use Token Authentication which is more secure in that it does not expose your Ably API key to your end clients.


2. Pub/Sub:

  • Using Realtime.co, you needed to subscribe and publish data to a channel in the following way:            
//subscribe
ortcClient.onConnected = function (ortc) {
  console.log("Connected to " + ortcClient.getUrl());
  ortcClient.subscribe('myChannel', true, function (ortc, channel, message) {
        console.log(message);
  });
};
//publish
var myMessage = { foo: "bar" };
ortcClient.send('myChannel', JSON.stringify(myMessage));  


  • Using Ably, you need to first attach to a channel, then subscribing and publishing data is quite similar as shown below:
//subscribe
var channel = realtime.channels.get("sport");
channel.subscribe(function(msg) {
  console.log("Received: " + JSON.stringify(msg.data));
});
//publish
channel.publish("update", { "team": "Man United" });


In both Realtime.co and Ably, you can set up subscribe filters. However, in Ably, it is very straight forward to do and is not different from a regular subscription unlike with Realtime.co. Read the complete documentation for more examples.


3. Presence:

Using Realtime.co, you need to enable presence and then request presence data as demonstrated in their documentation.

var ortcNodeclient = require('IbtRealTimeSJNode').IbtRealTimeSJNode;
var ortcClient = new ortcNodeclient();
 
ortcClient.enablePresence({
  applicationKey : '<ORTC_APPLICATION_KEY>',
  channel : 'myChannel',
  privateKey : '<ORTC_PRIVATE_KEY>',
  url : 'http://ortc-developers.realtime.co/server/2.1/',
  isCluster : true,
  metadata : true
},
function(error,result){
  if(error){
    console.log('Presence',error); 
  }else{
    console.log('Presence enable',result);
  }      
})


Using Ably, you can simply attach to a presence channel and subscribe to it in order to continue receiving updates.


var Ably = require('ably');
var realtime = new Ably.Realtime({
  key: 'xVLyHw.iMgyZQ:x1Z00B9Gk-_svk5B',
  clientId: 'bob' }
);
var channel = realtime.channels.get('bag-abs-tab');
channel.presence.subscribe('enter', function(member) {
  alert('Member ' + member.clientId + ' entered');
});
channel.presence.enter();


/* Subscribe to presence enter events */
channel.presence.subscribe('enter', function(member) {
  console.log(member.data); // => not moving
});

/* Subscribe to presence update events */
channel.presence.subscribe('update', function(member) {
  console.log(member.data); // => travelling North
});


4. Webhooks:

    Both Realtime.co's Webhooks feature as well as Ably's Webhooks feature are similar in their implementation.