I need to use nodejs next to php.
I have searched several forums including Stackoverflow but I still can not do it, my project is basically done in pure php but I want to use websockets to be able to implement a module of messages and notifications in real time.
I'm using Xampp for my project, I've already put the proxy URL in Apache but I can not get it to work. For example proxypass /nodejs
http//localhost:3000/socket.io/
before telling me cannot get/
after putting the proxy tells me cannot get/socket.io
the file is index.php
NOTE: If I change the file extension to index.html it works perfectly on real-time chat and everything.
I enclose an image with the error that I present:
and the code of my practice to achieve connection with nodejs and php:
My server in nodejs
const http = require('http');
const path = require('path');
const express = require('express');
const connect = require('connect');
const app = express();
const server = app.listen(3000);
const io = require('socket.io').listen(server);
//aqui estan los sockets desde otro archivo js para dejar el codigo de el servidor mas limpio
require('./socket')(io);
app.use(express.static(path.join(__dirname,'public')));
//voy a establecer un puerto si no tomas el 3000
app.set('port' , process.env.PORT || 3000)
//voy a recivir el puerto
server.listen(app.get('port') ,()=>{
console.log('conectado con el servidor '+app.get('port'))
})
The sockets
module.exports= function(io){
io.on('connection',socket=>{
console.log('usuarios conectados con io');
//esta es la accion que se recive desde el cliente
socket.on('mensaje-cliente',function (data) {
console.log(data);
//aqui el servidor la emite el mensaje a todos los clientes
io.sockets.emit('mensaje-servidor', data);
})
})
}
My index.html. If I change it to index.php it does not work
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>chat websocket</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="/css/main.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<nav class="navbar navbar-light bg-primary">
<a href="" class="navbar-brand mx-auto">WebsocketChat</a>
</nav>
<div class="container">
<div class="col-md-6 mt-4">
<div class="card">
<div class="card-header">
<h4>lets Chat</h4>
</div>
<div id="chat" class="card-body">
</div>
<form class="card-footer" action="index.html" method="post" id="message-form">
<div class="input-group">
<input type="text" name="" id="message" class="form-control">
<div class="input-group-append">
<input type="submit" class="btn btn-primary" name="" id="enviar">
</div>
</div>
</form>
</div>
</div>
</div>
//ya intente cambiarlo a http://localhost:3000 pero nada de nada
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script src="/js/main.js"></script>
</body>
</html>
micliente main.js
$(function () {
var socket=io();
//intente poner esta linea en el cliente pero tampoco
//var sockets = io.connect( 'http://localhost:' );
//obteniendo los elementos del Dom
var formulario = $("#message-form");
var mensajebox = $("#chat");
var mensaje = $("#message");
var enviar = $("#enviar");
formulario.submit( e =>{
e.preventDefault();
//console.log(mensaje.val());
//aquii con socket.io le damos un nombre al evento y capturamos el mensaje y *
socket.emit('mensaje-cliente',mensaje.val());
//limpiar input despues de enviar
mensaje.val("");
});
/*aqui nos toca escuchar el mensaje atraves de la palabra clave del servidor*/
socket.on('mensaje-servidor',function(data){
//aqui elegimos donde mostrar el mensaje del servidor
mensajebox.append(data+'<br>');
});
})
Please, if someone is so kind to help me, I would really appreciate it. Any tip or advice from someone who has experience in integrating nodejs with php is well received.