简体   繁体   English

使用node.js向API发出请求

[英]Making requests to API using node.js

I'm developing an API in PHP using the micro application feature from Phalcon Framework. 我正在使用Phalcon Framework的微应用程序功能在PHP中开发API。 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. 此API包括接收GET,POST,PUT,DELETE请求,然后以JSON格式从MySQL数据库(API可以访问数据库)返回结果。

I'm developing a phonegap + ionic framework mobile application too and I need to connect all mobile users in realtime. 我正在开发一个phonegap +离子框架移动应用程序,我需要实时连接所有移动用户。 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. 像聊天一样,从API返回的信息,例如:所有配置文件信息,如姓名,电子邮件,年龄,生日将使用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? 我的问题是,这里有可能实现node.js,使应用程序实时使用API​​返回,插入,更新数据? 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. 我需要在用户之间创建交互,例如:有人会实时向其他用户发出朋友请求,如果请求的用户接受请求,则使用api更新mysql数据库,将用户添加到好友列表中。

I want to use API because I don't want to give the future developers, the database access. 我想使用API​​,因为我不想给未来的开发人员,数据库访问。 But performance is my priority. 但性能是我的首要任务。

Thanks! 谢谢!

Node.js is a good choice to make a realtime framework to link your users to your RESTful backend. Node.js是制作实时框架以将用户链接到RESTful后端的不错选择。 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. 但是,您也可以考虑使用托管的实时消息服务(如PubNub)实时在用户和PHP后端之间传递数据。

Using PubNub's PHP Api , you can set up your server to listen for events: 使用PubNub的PHP Api ,您可以设置服务器以侦听事件:

$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: 我将从JavaScript SDK中给出一个示例,但是每个大型移动平台都有一个SDK:

     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. 我们称之为PubNub Access Manager,它受到了很大的支持。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM