Solution 1 :

Try

// Allow all origins
io.origins('*');

source

Problem :

I’m having trouble migrating my locally hosted chat application using Socket.io to my live cloud server. I am aware there are solutions out there, however, I cannot find anything that solves my problem.

I am receiving “Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at…. (Reason: CORS request did not succeed)”
”’

const io = require('socket.io')(3000)

io.on('connection', socket => {
    socket.on('new-user', name => {
        users[socket.id] = name
        socket.broadcast.emit('user-connected', name)
    })
    socket.on('send-chat-message', message => {
        socket.broadcast.emit('chat-message', {message: message, name: 
            users[socket.id]})
    })
    socket.on('disconnect', () => {
        socket.broadcast.emit('user-disconnected', users[socket.id])
       delete users[socket.id]

    })  

}) 

Above is the server.js file.

    const socket = io('<url>:3000')
    const messageForm = document.getElementById('send-container')
    const messageInput = document.getElementById('message-input')
    const messageContainer = document.getElementById('message-container')

This is the script my app is using, the is replaced by mine, it just masked on here.

Things I have tried:

  • Setting headers using “Header set Access-Control-Allow-Origin” in my Apache & Nginx config
  • Changing the url on the script
  • Changing the ports

So far I’ve no luck, please help me!!

By

Leave a Reply

Your email address will not be published. Required fields are marked *