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://github.com/swoole/swoole-src
https://github.com/reactphp/socket
any other framework?
But I don't know if it is a good and stable alternative?
Thanks!
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.
Try phpsocket.io 1 , 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);
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.