node.js - nodeJS and PHP (Laravel) integration for Socket.IO live chat -


currently have website wrote on php via laravel framework. have wrote live chat using nodejs socket.io , express , want integrate inside written laravel website. problem chat must in main page, rendered views of laravel. on shared hosting.

the question: best suggestions such integration? know lamp stack comes ready in shared domains have no idea how php(laravel) , nodejs chat work together.

things have tried:

  • elephant.io - didn't have big success yet...

the solution simple (but finding on internet not). need include socket.io js file in html view of php, socket.io js files makes connection node.js server. works fine on localhost. however, if else tries log chat outside, experience "forbidden crossdomain request" error, because have followed "guide" me , socket.io connection in client that:

var socket = io.connect('localhost:8080'); 

instead of

var baseurl               = getbaseurl(); // call function determine var socketioport          = 8080; var socketiolocation      = baseurl + socketioport; // build socket.io location var socket                = io.connect(socketiolocation);  // build user-specific path socket.io server, works both on 'localhost' , 'real domain' function getbaseurl() {     baseurl = location.protocol + "//" + location.hostname + ":" + location.port;     return baseurl; } 

the php client code is:

<!doctype html> <html lang="en"> <head>   <meta charset="utf-8"> </head> <body>    <!-- wrapper-->   <div id="wrapper">      <!-- chat: input -->     <div id="chat-input">        <!-- username -->       <div class="username">         <p id="username">john doe</p>       </div>        <!-- form -->       <form action="">          <!-- input field -->         <input type="text" class="chat_input-message" id="message" placeholder="enter message..." autocomplete="off" autofocus="on" />          <!-- button -->         <button>send</button>        </form>       <!-- end: form -->     </div>     <!-- end chat: input -->      <div id="chat-output">       <div id="messages"></div>     </div>    </div>   <!-- end: wrapper -->    <!-- scripts -->   <!-- socket.io -->   <script src="../node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js"></script>   <!-- jquery -->   <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>   <!-- chat -->   <script src="../public/js/chat.js"></script>   <!-- end: scripts -->  </body> </html> 

the server-side node.js code not need tweaks, forget redis or in php (elephant.io, ajax random injects, forget hacks). works magic.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -