Laravel IO Socket and redis over SSL https connection -


currently have io sockets laravel broadcasting redis working perfectly. until set ssl cert on domain.

i have redis-server running on port 3001.

then there socket.js set listen 3000.

my js on page listen via io('//{{ $listen }}:3000').

any guidance great on how working on https. use 443 port?

thanks.

my socket.js

var app = require('express')(); var http = require('http').server(app); var io = require('socket.io')(http); var redis = require('ioredis'); var redis = new redis(); redis.subscribe('notifications', function(err, count) { }); redis.on('message', function(channel, message) {     console.log('message recieved: ' + message);     message = json.parse(message);     io.emit(channel + ':' + message.event, message.data); }); http.listen(3000, function(){     console.log('listening on port 3000'); }); 

first, setup serveroptions object:

var serveroptions = {     port: 3000,     host: 127.0.0.1, //address site     key: '/etc/nginx/ssl/your_site/server.key', //or whatever path ssl     cert: '/etc/nginx/ssl/your_site/server.crt',     npnprotocols: ['http/2.0', 'spdy', 'http/1.1', 'http/1.0'] } 

for npnprotocols, may not care of them, they're provided reference.

now create server:

var app = require('https').createserver(serveroptions),     io = require('socket.io')(app); 

this should pretty plug , play source @ point.\

as side note stuff wide open , can listen on web socket, nothing should sent private through here. if need make data private, you're going need 1 of 2 things;

  1. something jwt-auth token
  2. something custom interface redis queue:

here's example of latter:

var secret_key = '<your_laravel_secret_key>'; var laravel_session_parser = {     ord: function (string) {         return string.charcodeat(0);     },     decryptsession: function (cookie, secret) {         if (cookie) {             var session_cookie = json.parse(new buffer(cookie, 'base64'));             var iv = new buffer(session_cookie.iv, 'base64');             var value = new buffer(session_cookie.value, 'base64');             var rijcbc = new mcrypt.mcrypt('rijndael-128', 'cbc');             rijcbc.open(secret, iv);             var decrypted = rijcbc.decrypt(value).tostring();             var len = decrypted.length - 1;             var pad = laravel_session_parser.ord(decrypted.charat(len));             return phpunserialize.unserialize(decrypted.substr(0, decrypted.length - pad));         }         return null;     },     getuidfromobj: function (obj, pattern) {         var regexp = /login_web_([a-za-z0-9]+)/gi;         if (pattern) {             regexp = pattern;         }         var u_id = null;         (var key in obj) {             var matches_array = key.match(regexp);             if (matches_array && matches_array.length > 0) {                 u_id = obj[matches_array[0]];                 return u_id;             }         }         return u_id;     },     getredissession: function (s_id, cb) {         var _sessionid = 'laravel:' + s_id;         client.get(_sessionid, function (err, session) {             if (err) {                 cb && cb(err);                 return;             }             cb && cb(null, session);         });     },     getsessionid: function (session, _callback) {         var u_id = null,             err = null;         try {             var laravelsession = phpunserialize.unserialize(phpunserialize.unserialize(session));             u_id = laravel_session_parser.getuidfromobj(laravelsession);         } catch (err) {             _callback(err, null);         }         _callback(err, u_id);     },     ready: function (socket, _callback) {         if (typeof socket.handshake.headers.cookie === 'string') {             var cookies = cookie.parse(socket.handshake.headers.cookie);             var laravel_session = cookies.laravel_session;             var session_id = laravel_session_parser.decryptsession(laravel_session, secret_key);              laravel_session_parser.getredissession(session_id, function (err, session) {                 if (!err && session) {                     laravel_session_parser.getsessionid(session, function (err, user_id) {                         if (user_id) {                             _callback(null, session_id, user_id, laravel_session)                         } else {                             _callback(new error('authentication error'), null);                         }                     });                 } else {                     _callback(new error('authentication error'), null);                 }             });         }     } }; 

now can have io instance of individuals session when establish connection socket.io

io.on('connection', function (socket) {     laravel_session_parser.ready(socket, function(err, session_id, user_id, laravel_session) {         //log out variables above see provide     }); }); 

note, prefer use dotenv in nodejs share environment variables between laravel , node.

then can process.env.app_key , don't need worry sharing variables.

also of note, script above not complete , not production ready, it's meant used example.


Comments

  1. @admin
    is their any possibility to add script in website and change the default template.
    Regards,
    Deepika Verma

    ReplyDelete

Post a Comment

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -