11

Currently I have a website which I wrote on PHP via the Laravel framework. I have wrote a live chat using nodeJS with Socket.IO and Express and now what I want to do is to integrate it inside my already written Laravel website. The problem is the chat must be in the main page, which is currently rendered by the views of Laravel. Currently I am on a shared hosting.

The question: What are your best suggestions for such integration? I know that the LAMP stack comes ready in most shared domains but I have completely no idea how I am to get PHP(Laravel) and my nodeJS chat to work together.

Things I have tried:

  • Elephant.IO - Didn't have any big success with it yet...
11
  • 1
    Shared hosting will be a problem. Although there is a bit of a learning curve, having your own VPS gives you significantly more freedom in how you implement these things. These cost as little as $5 a month, similar to shared hosting. Commented Aug 18, 2014 at 15:46
  • Do these two work on your development environment and you're looking at ways to tie them together from a deployment aspect? Commented Aug 18, 2014 at 15:47
  • @tadman - Yes, exactly. Commented Aug 19, 2014 at 6:26
  • OK, I have been continuing to read, test, and fail, fail and FAIL and here is what I think so far: since Socket.IO has both client-side part (the JavaScript include file rendered in the browser) and a server-side (the socket.io npm module installed in Node, correct me if I am wrong) what I need to do is probably basically "make PHP talk to the socket.IO" loaded in the browser. Commented Aug 21, 2014 at 11:20
  • Without any code examples, this is really hard to provide specific advice on. How do you have them linked together on your development system? What domain are you using for testing? Commented Aug 21, 2014 at 11:26

1 Answer 1

8

the solution is simple (but finding ANYTHING about it on the internet was not). You just need to include your socket.io JS file in the HTML view of PHP, then the socket.io JS files makes a connection to your node.JS server. This works all fine on localhost. However, if someone else tries to log into your chat from outside, they will experience a "Forbidden crossdomain request" error, which is because you have probably followed some "guide" like me and your socket.io connection in the CLIENT is like that:

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

instead of

var baseURL               = getBaseURL(); // Call function to determine it
var socketIOPort          = 8080;
var socketIOLocation      = baseURL + socketIOPort; // Build Socket.IO location
var socket                = io.connect(socketIOLocation);

// Build the user-specific path to the socket.io server, so it works both on 'localhost' and a '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 your 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 does not need any tweaks, forget everything about Redis or in PHP (Elephant.IO, AJAX random injects, forget about any hacks). It simply works as a magic.

Sign up to request clarification or add additional context in comments.

3 Comments

yeah, providing you're doing something non trivial like a chat. i can forge my headers and send any arbitrary message i want to your socket server and it will happily relay it to all clients. this is why you must use pub/priv key (hmac is best) before relaying any message to client. only the server shall have permission to write to the socket, never a client. client shall only receive.
@r3wt do you have an example or tutorial link to what you're describing regarding pub/priv key (hmac). I've just made a similar chat, but I don't understand the insecurities that surround using it in an application.
@mtpultz if your websocket server broadcasts any message it receives, its already insecure. this is why pusher and pubnub require secret keys to write,and a public key to read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.