Sending messages from PHP to Node.js

后端 未结 6 2014
终归单人心
终归单人心 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:44

    Step 1. Get the PHP Emitter: https://github.com/rase-/socket.io-php-emitter

    $redis = new \Redis(); // Using the Redis extension provided client
    $redis->connect('127.0.0.1', '6379');
    $emitter = new SocketIO\Emitter($redis);
    $emitter->emit('new question', 'h
    tml
    ');

    add this to your index.js:

    var redis = require('socket.io-redis');
    io.adapter(redis({ host: 'localhost', port: 6379 }));
    io.on('connection', function(socket){
        socket.on('new question', function(msg) {
            io.emit('new question', msg);
        });
    });
    

    add something like this to your index.html

    socket.on('new question', function(msg) {
        $('body').append( msg );
    });
    

提交回复
热议问题