'Elephant.io server error with Php( An error occurred while trying to establish a connection to the server)
I have a question for you, I'm using socket.io and elephant.io in my php application, but I've been dealing with the error I got by elephant.io for almost 2 days. Is there something I did wrong? Below are the errors and codes I got.
MY ERROR enter image description here
MY INDEX.PHP
require __DIR__ . '/vendor/autoload.php';
use ElephantIO\Client;
use ElephantIO\Engine\SocketIO\Version2X;
$client = new Client(new Version2X('http://localhost:9090'));
$client->initialize();
$client->emit('send-message', [
'name' => 'john',
'surname' => 'doe'
]);
$client->close();
MY APP.JS
const server = require('http').createServer();
const io = require('socket.io')(server,{
cors: {
origin: "*",
}
});
io.on('connection',function (socket){
console.log('connected');
socket.on('send-message',function (data){
console.log(data);
io.emit('message',data);
})
socket.on('disconnect',function (){
console.log('disconnected')
})
})
server.listen(9090);
MY SOCKET.HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.2.0/socket.io.js"
integrity="sha512-WL6WGKMPBiM9PnHRYIn5YEtq0Z8XP4fkVb4qy7PP4vhmYQErJ/dySyXuFIMDf1eEYCXCrQrMJfkNwKc9gsjTjA=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
var socket = io('http://localhost:9090');
socket.on('message',function (data){
console.log(data);
});
socket.emit('send-message',{
"name":"johnHTML",
"surname": "doeHTML"
});
</script>
</body>
</html>
Solution 1:[1]
I had this error once. I searched like you but couldn't find it. I tried many different methods, none of them worked. As a last resort I changed the version of the "socket.io" library to 2.2.0 and it worked. you should try this
Solution 2:[2]
You can use this updated code of elephant.io without downgrading your socket version. it supports socket version more than 3
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | thecodepanda |
Solution 2 | hiren.viradia |