Socket.io redirects to public/socket.io – 301 on cPanel

Published

I have created a chat functionalities for a project using Laravel + Vue + Socket.io, locally everything is working fine but when I upload on cPanel live Chat is not working, below is screenshot of Network tab for reference https://prnt.sc/1m2qyap

Note: On cPanel to run server.js, I have created app using Application Manager

Here is my server.js created for socket.io

const express = require("express");
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server,
    {
        cors: { origin : "*" }
    }
);

io.on('connection', (socket) =>{    
    console.log("Connected...");

    socket.on('sendChatToServer',(message) =>{
        console.log(message);
        io.sockets.emit('sendChatToClient',message);
    })       
    
    io.emit('noOfConnections', Object.keys(io.sockets.connected).length)
    socket.on('disconnect', () => {
        console.log('disconnected')        
    })
    socket.on('chat-message', (msg) => {
        console.log(msg);
        socket.broadcast.emit('chat-message', msg)
    })
    socket.on('joined', (name) => {
        console.log(name+' joined');
        socket.broadcast.emit('joined', name)
    })
    socket.on('leaved', (name) => {
        socket.broadcast.emit('leaved', name)
    })
    socket.on('typing', (data) => {
        socket.broadcast.emit('typing', data)
    })
    socket.on('stoptyping', () => {
        socket.broadcast.emit('stoptyping')
    })
})

server.listen(3000, function(){
    console.log('Listening to port 3000');
});

Here is app.js code in which I have register

// ******SOCKET.IO REGISTRATION***********
import io from 'socket.io-client';
window.io = io;
window.socket = io('http://localhost:3000');

Here is chat.js page code in which I have execute chat_message event.

socket.on("chat-message", data => {
      if (this.auth_user.id == data.receiver_id) {
        this.chat_list.push({
          message: data.message,
          sender_id: data.sender_id,
          send_from: data.send_from,
          receiver_id: data.receiver_id
        });
      }
      this.sender_id = data.sender_id;
      this.receiver_id = data.receiver_id;
});

Source: Laravel

Published
Categorised as chat, laravel, socket.io, vue.js Tagged , , ,

Answers

Leave a Reply

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq