Sending messages from PHP to Node.js

后端 未结 6 2007
终归单人心
终归单人心 2020-12-04 20:25

How to send messages from php to node.js? I have a linux server running php and node.js.

When a user completes a transaction (via php), I\'d like send a message f

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 20:47

    A bit late, but you could communicate with your node client using the Redis Pub/Sub mechanism in a very simple and effective way. All you need to do is install redis on your server.

    On the php side, initialize Redis then publish a message

    $purchase_info = json_encode(array('user_id' =>$user_id,
             'purchase_information'=>array('item'=>'book','price'=>'2$'));
    
    $this->redis->publish('transaction_completed', $purchase_info);
    

    On the node.js side

    var redis = require('redis');
    var purchase_listener = redis.createClient();
    purchase_listener.subscribe('transaction_completed');
    purchase_listener.on('message', function(channel, message){
        var purchase_data = JSON.parse(message);
        user_id = purchase_data.user_id;
        purchase_info = purchase_data.purchase_information;
        // Process the data
        // And send confirmation to your client via a socket connection
    })
    

    Is this scalable ? (In response to @mohan-singh)

    When talking about scalability you need to think about your infrastructure's architecture and your particular needs but here's a quick answer : I've been using a variant of this mechanism on a high traffic real-time application without problems but here's what you should be careful about:

    1. Redis PUB/SUB is not a queuing system, that means if your node process goes down all the messages that were sent WHILE it is down will be lost.

    2. If you have more than 1 subscriber to the publisher they will all receive the same message and handle it, be careful about that if you have more than a node process listening to the same redis db handling your real-time logic (There are easy ways to go around this though)

    The nice thing about this system is that you don't need to add anything to your existing infrastructure and can get started immediately, it's very fast and it behaves exactly like an HTTP server.

    Here are your alternatives for more scalable options:

    1. Using a self-hosted fast messaging queue server (ActiveMQ, RabbitMQ, beanstalkd ... ) server to handle your messaging logic between php and node, these tend to be fast but as the load increases you lose a bit of performance, and have to maintain/scale your messaging servers, and take care of duplication across regions which is not an easy and enjoyable thing (depending on what you enjoy doing).
    2. Using a hosted messaging queue server (IronMQ, SQS...) Some of these(IronMQ) are pretty fast and will be great for your use case but introduce some (minor) complexity to your codebase.
    3. Building a messaging queue with Redis with clustered node servers : https://davidmarquis.wordpress.com/2013/01/03/reliable-delivery-message-queues-with-redis/
    4. Using HTTP inside a VPN to communicate with node servers. Once you see your traffic spiking you will only need to load-balance your node servers and add as much stateless servers as you need and send POST messages to that load balancer.

    The point of this lengthy edit is that there is no such thing as a magic scalable solution, you need to weigh your options and see which one works the best for your use case. In my opinion, if you're starting to build your first iteration now, choose any option that you're comfortable with, write very clean code and when you start scaling it will be very easy to change, this is what I've done :)

提交回复
热议问题