0

I just downloaded and ran the very basic Node.js chat app:

https://github.com/socketio/chat-example

It is working properly.

The server code is very simple:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
  });
});

http.listen(port, function(){
  console.log('listening on *:' + port);
});

Now my question is if is it possible to do something similar with PHP?

I have heard about:

https://www.swoole.co.uk

https://github.com/swoole/swoole-src

https://reactphp.org

https://github.com/reactphp/socket

any other framework?

But I don't know if it is a good and stable alternative?

Thanks!

2
  • Try: php.net/manual/en/sockets.examples.php Commented Nov 3, 2018 at 2:50
  • It is commonly discouraged due to the limitations of PHP being run as a daemon, and environmental constraints but is suitable for smaller applications I have implemented a few PHP implementations for Programmable Logic Controllers to send and receive instructions over UDP. I would advise looking at other languages like Java to accomplish it though. Commented Nov 3, 2018 at 2:56

2 Answers 2

1

There is a library called Ratchet i know about. But in my experience, it does not work very well. If you are trying to make a socket connection, you should always just technologies that supports it very well, such as NodeJs

It's possible to call a NodeJS server (express) from your PHP using a curl request, and make the NodeJS server do the socket connection.

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

Comments

0

Try phpsocket.io1, it is a direct PHP implementation of socket.io and even has the excellent example chat app. It's rock solid and well maintained. Personally, I had nothing but trouble with other approaches I tried.

Your example code would look something like this in PHP:

$io->on('connection', function($socket){
  $socket->on('chat message', function($msg){
    $io->emit('chat message', $msg);
  });
});

Comments

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.