Thyoity
Thyoity

Reputation: 191

Making requests to API using node.js

I'm developing an API in PHP using the micro application feature from Phalcon Framework. This API consists in receiving GET, POST, PUT, DELETE requests then return results from the MySQL database (the API has access to the database) in JSON format.

I'm developing a phonegap + ionic framework mobile application too and I need to connect all mobile users in realtime. Something like a chat, with informations returning from the API, for example: All profile informations like name, email, age, birthday will be stored and retrieved using the API.

My question is, its all possible to implement node.js here to make the app real time even using the API to return, insert, update data? I'll need to create interactions between users, example: Someone will make a friend request to another user in real time, if the requested user accept the solicitation, it uses the api to update the mysql database, adding the user to the friendlist.

I want to use API because I don't want to give the future developers, the database access. But performance is my priority.

Thanks!

Upvotes: 3

Views: 1216

Answers (1)

drnugent
drnugent

Reputation: 1545

Node.js is a good choice to make a realtime framework to link your users to your RESTful backend. However, you may also consider using a hosted realtime messaging service such as PubNub to pass data between your users and your PHP backend in real time.

Using PubNub’s PHP Api, you can set up your server to listen for events:

$pubnub = new Pubnub(
    "demo",  ## PUBLISH_KEY
    "demo",  ## SUBSCRIBE_KEY
    "",      ## SECRET_KEY
    false    ## SSL_ON?
);
$pubnub->subscribe(array(
    'channel'  => 'hello_world',        ## REQUIRED Channel to Listen
    'callback' => function($message) {  ## REQUIRED Callback With Response
        ## Do all the awesome stuff your server does
        return true;         ## Keep listening (return false to stop)
    }
));

Now that your server is subscribed to your channel, you can have your clients subscribe as well, to listen for global events. I’m going to give an example from the JavaScript SDK, but there’s an SDK for every sizable mobile platform as well:

     var pubnub = PUBNUB.init({
         publish_key   : 'demo',
         subscribe_key : 'demo'
     })

     function publish() {
         pubnub.publish({
             channel : "hello_world",
             message : "Bob added Stan as a friend"
         })
     }
 })();

You can also do this in reverse, to broadcast messages from the server to the clients. Bam!

Eventually you may want to extend your app with a unique channel for each user to be able to communicate privately with the server, and also authentication; we call this PubNub Access Manager and it is heavily supported.

Good luck!

Upvotes: 3

Related Questions