在https服务器中我们需要配置什么才能实现实时通知模块(使用redis和socket.io)?

时间:2017-10-20 10:04:12

标签: php laravel sockets laravel-5 laravel-5.4

我在本地计算机上完成了一个成功的实时通知模块。我需要在live server中实现这个模块。实时服务器是https。我不知道我们需要为https更改哪些内容。请检查我的代码并帮助将其集成到https服务器上。感谢。

ssl文件

SSLCertificateFile /etc/letsencrypt/live/dev.test.de/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/dev.test.de/privkey.pem

socket.js

var app   = require('express')();
var http  = require('http').Server(app);
var io    = require('socket.io')(http);
var redis = require('redis');

io.on('connection', function(socket){
    console.log('a user connected');

    var redisClient = redis.createClient();

    /* Realtime message subscribe */
    redisClient.subscribe('message');
    redisClient.on('message', function(channel, message){
        /*console.log('new message in queue', channel, message);*/
        socket.emit(channel, message);
    });

    /* Realtime inquiry subscribe */
    redisClient.subscribe('inquiryCount');
    redisClient.on('inquiryCount', function(channel, message){
        /*console.log('new inquiry in queue', channel, message);*/
        socket.emit(channel, message);
    });

    socket.on('disconnect', function(){
        redisClient.quit();
        console.log('user disconnected');
    });
});


http.listen(3000, function(){
    console.log('listening on *:3000');
});

app.blade.php

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.8/socket.io.min.js"></script>
    <script>
        var socket = io('{{env('APP_URL')}}:3000');
        /* Realtime message notification */
        socket.on('message', function(data){
           if(data){
               /* var res = $.parseJSON(data);*/
               $('.messages-menu').empty();
               $('.messages-menu').html(data);
           }
        });
        /* Realtime inquiry notification */
        socket.on('inquiryCount', function(data){
            if(data){
                $('.tasks-menu').empty();
                $('.tasks-menu').html(data);
            }
        });
    </script>

DashboardController.php

public function privateMessageAPICount($id)
{
      $message = 'taking count of data';
      if(count($message) > 0){
           $redis = Redis::connection();
                $redis->publish('message', $message);
                return response()->json(['status' => 'success'], 200);
        }
        else {
            return response()->json(['status' => 'No Result'], 404);
        }
 }

public function inquiryAPIUnreadCount($id)
{
   $inquiryCount .= 'taking count of data';
   if(count($inquiryCount ) > 0){
         $redis = Redis::connection();
          $redis->publish('inquiryCount', $inquiryCount);
          return response()->json(['status' => 'success'], 200);
        }
        else {
            return response()->json(['status' => 'No Result'], 404);
        }              
 }

4 个答案:

答案 0 :(得分:7)

当您的apache服务器已经使用https配置时,您只需要对您的ws服务器实施https。由于这是节点http服务器,因此修改后的代码应该可用(基于此post):

var app   = require('express')();
var fs = require('fs');
var privateKey  = fs.readFileSync('/etc/letsencrypt/live/dev.test.de/privkey.pem', 'utf8');
var certificate = fs.readFileSync('/etc/letsencrypt/live/dev.test.de/fullchain.pem', 'utf8');
var credentials = {key: privateKey, cert: certificate};
var https  = require('https').createServer(credentials, app);
var io    = require('socket.io')(https);
var redis = require('redis');


io.on('connection', function(socket){
    console.log('a user connected');

    var redisClient = redis.createClient();

    /* Realtime message subscribe */
    redisClient.subscribe('message');
    redisClient.on('message', function(channel, message){
        /*console.log('new message in queue', channel, message);*/
        socket.emit(channel, message);
    });

    /* Realtime inquiry subscribe */
    redisClient.subscribe('inquiryCount');
    redisClient.on('inquiryCount', function(channel, message){
        /*console.log('new inquiry in queue', channel, message);*/
        socket.emit(channel, message);
    });

    socket.on('disconnect', function(){
        redisClient.quit();
        console.log('user disconnected');
    });
});


https.listen(3000, function(){
    console.log('listening on *:3000');
});

您的APP_URL必须以https://开头。

我建议使用laravel-echo-server来处理ws-connections,它更简单

答案 1 :(得分:4)

我不能把它写成命令@sam sam。

只需要做的就是

    http{
    server {
            listen       443 ssl;
            server_name  localhost;
            keepalive_timeout  5;
            proxy_connect_timeout 7d;
            proxy_send_timeout 7d;
            proxy_read_timeout 7d;

            # Self-signed certificate.
            ssl_certificate ../ssl/server.crt;
            ssl_certificate_key ../ssl/server.key;
            ssl_protocols         SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2;

            location / {
                proxy_pass http://apache_nodes/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_redirect off;

                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
    }
       upstream apache_nodes {
        server 192.168.1.155;
    }

}

这将保证您处理443并通过代理通行证将客户发送到80。

有关更多信息,请查看nginx ssl代理服务器。使用相同的代理服务器用法,您还可以处理443以上的socket.io请求

这个

 upstream socket_nodes_1 {
        server 192.168.1.155:1337;
    }
location /sockets {
            proxy_pass http://socket_nodes_1/;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_redirect off;

            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

此致

答案 2 :(得分:3)

这是我的工作socket_https.js:

const fs = require('fs');

const options = {
    key: fs.readFileSync('/etc/letsencrypt/live/dev.test.de/privkey.pem'),
    cert: fs.readFileSync('/etc/letsencrypt/live/dev.test.de/fullchain.pem'),
    NPNProtocols: ['http/2.0', 'spdy', 'http/1.1', 'http/1.0']
};

var server = require('https').Server(options);

var io = require('socket.io')(server);


var Redis = require('ioredis');
var redis = new Redis();

redis.subscribe('message-channel');

redis.on('message', function (channel, message) {
    console.log('Message Receive');
    console.log(message);
    message = JSON.parse(message);

    io.emit(channel + ':' + message.event, message.data);
});

server.listen(4000);

然后当我想发布到redis:

$data = [
      'event' => 'someEvent',
      'data'  => [
                      'id' => $id,
                      [...]
                  ]
];

Redis::publish('my-channel', json_encode($data));

答案 3 :(得分:0)

不要改变你身边的任何东西。仅使用nginx处理https并在位置/使用proxypass到80端口。顺便说一句,您的系统现在支持并处理https