Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h1>Update</h1> <p>Requirements:</p> <ol> <li>First have redis running.</li> <li>Next fire up socket.io.</li> <li>Finally upload/host PHP(has dependencies in archive).</li> </ol> <h2>Socket.io</h2> <pre><code>var express = require('express'), app = express.createServer(), sio = require('socket.io'), redis = require("redis"), client = redis.createClient(), io = null; /** * Used to parse cookie */ function parse_cookies(_cookies) { var cookies = {}; _cookies &amp;&amp; _cookies.split(';').forEach(function( cookie ) { var parts = cookie.split('='); cookies[ parts[ 0 ].trim() ] = ( parts[ 1 ] || '' ).trim(); }); return cookies; } app.listen(3000, "localhost"); io = sio.listen(app); io.of('/private').authorization(function (handshakeData, callback) { var cookies = parse_cookies(handshakeData.headers.cookie); client.get(cookies.PHPSESSID, function (err, reply) { handshakeData.identity = reply; callback(false, reply !== null); }); }).on('connection' , function (socket) { socket.emit('identity', socket.handshake.identity); }); </code></pre> <h2>PHP</h2> <p>php with openid authentication => <a href="http://dl.dropbox.com/u/314941/6503745/php.tar.gz" rel="nofollow noreferrer">http://dl.dropbox.com/u/314941/6503745/php.tar.gz</a></p> <p>After login you have to reload <code>client.php</code> to authenticate</p> <hr> <p><strong>p.s: I really don't like the concept of creating even another password which is probably is going to be unsafe. I would advice you to have a look at <a href="http://en.wikipedia.org/wiki/OpenID" rel="nofollow noreferrer">openID</a>(via <a href="https://stackoverflow.com/questions/4459509/how-to-use-open-id-as-login-system/4460342#4460342">Google</a> for example), <a href="https://developers.facebook.com/docs/guides/web/" rel="nofollow noreferrer">Facebook Connect</a>(just name a few options).</strong></p> <blockquote> <p>My question is once they authenticate via php/session what would be the process to authenticate the user to see if they have the right login permissions to access a nodejs server with socket.io? I dont want the person to have access to the nodejs/socket.io function/server unless they have authenticated via the php login.</p> </blockquote> <p>Add the unique <a href="http://php.net/manual/en/function.session-id.php" rel="nofollow noreferrer">session_id</a> to a list/set of allowed ids so that socket.io can <a href="https://github.com/LearnBoost/Socket.IO-node/wiki/Configuring-Socket.IO" rel="nofollow noreferrer">authorize</a>(search for authorization function) that connection. I would let PHP communicate with node.js using <a href="http://redis.io" rel="nofollow noreferrer">redis</a> because that is going to be lightning fast/AWESOME :). Right now I am faking the PHP communication from <code>redis-cli</code></p> <h2>Install Redis</h2> <p><a href="http://redis.io/download" rel="nofollow noreferrer">Download redis</a> => Right now the stable version can be downloaded from: <a href="http://redis.googlecode.com/files/redis-2.2.11.tar.gz" rel="nofollow noreferrer">http://redis.googlecode.com/files/redis-2.2.11.tar.gz</a></p> <pre><code>alfred@alfred-laptop:~$ mkdir ~/6502031 alfred@alfred-laptop:~/6502031$ cd ~/6502031/ alfred@alfred-laptop:~/6502031$ tar xfz redis-2.2.11.tar.gz alfred@alfred-laptop:~/6502031$ cd redis-2.2.11/src alfred@alfred-laptop:~/6502031/redis-2.2.11/src$ make # wait couple of seconds </code></pre> <h2>Start Redis-server</h2> <pre><code>alfred@alfred-laptop:~/6502031/redis-2.2.11/src$ ./redis-server </code></pre> <h1>Socket.io</h1> <h2>npm dependencies</h2> <p>If <code>npm</code> is not already installed , then first visit <a href="http://npmjs.org" rel="nofollow noreferrer">http://npmjs.org</a></p> <pre><code>npm install express npm install socket.io npm install redis </code></pre> <p>listing the dependencies I have installed and which you should also probably install in case of incompatibility according to <code>npm ls</code></p> <pre><code>alfred@alfred-laptop:~/node/socketio-demo$ npm ls /home/alfred/node/socketio-demo ├─┬ express@2.3.12 │ ├── connect@1.5.1 │ ├── mime@1.2.2 │ └── qs@0.1.0 ├── hiredis@0.1.12 ├── redis@0.6.0 └─┬ socket.io@0.7.2 ├── policyfile@0.0.3 └── socket.io-client@0.7.2 </code></pre> <h1><a href="https://gist.github.com/1050729" rel="nofollow noreferrer">Code</a></h1> <h2><a href="https://gist.github.com/raw/1050729/560c01977d8497c9db17090e5e7e499085505f7a/app.js" rel="nofollow noreferrer">server.js</a></h2> <pre><code>var express = require('express'), app = express.createServer(), sio = require('socket.io'), redis = require("redis"), client = redis.createClient(), io = null; /** * Used to parse cookie */ function parse_cookies(_cookies) { var cookies = {}; _cookies &amp;&amp; _cookies.split(';').forEach(function( cookie ) { var parts = cookie.split('='); cookies[ parts[ 0 ].trim() ] = ( parts[ 1 ] || '' ).trim(); }); return cookies; } app.listen(3000, "localhost"); io = sio.listen(app); io.configure(function () { function auth (data, fn) { var cookies = parse_cookies(data.headers.cookie); console.log('PHPSESSID: ' + cookies.PHPSESSID); client.sismember('sid', cookies.PHPSESSID, function (err , reply) { fn(null, reply); }); }; io.set('authorization', auth); }); io.sockets.on('connection', function (socket) { socket.emit('access', 'granted'); }); </code></pre> <p>To run server just run <code>node server.js</code></p> <h2><a href="https://gist.github.com/raw/1050729/00f103555cb6136d478a4e699e595f58c8f267ea/client.php" rel="nofollow noreferrer">client.php</a></h2> <pre><code>&lt;?php session_start(); echo "&lt;h1&gt;SID: " . session_id() . "&lt;/h1&gt;"; ?&gt; &lt;html&gt; &lt;head&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="http://localhost:3000/socket.io/socket.io.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;p id="text"&gt;access denied&lt;/p&gt; &lt;script&gt; var socket = io.connect('http://localhost:3000/'); socket.on('access', function (data) { $("#text").html(data); }); &lt;/script&gt; &lt;/body&gt; </code></pre> <h2>Test authentication</h2> <p>When you load the webpage(PHP-file) from your web-browser the message <code>access denied</code> is shown, but when you add the <code>session_id</code> also shown in browser to redis server the message <code>access granted</code> will be shown. Of course normally you would not be doing any copy pasting but just let PHP communicate with Redis directly.<img src="https://i.stack.imgur.com/ANN8C.jpg" alt="auth">. But for this demo you will put SID <code>ramom807vt1io3sqvmc8m4via1</code> into redis after which access has been granted.</p> <pre><code>alfred@alfred-laptop:~/database/redis-2.2.0-rc4/src$ ./redis-cli redis&gt; sadd sid ramom807vt1io3sqvmc8m4via1 (integer) 1 redis&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload