Skip to content

Instantly share code, notes, and snippets.

@Carlsson87
Last active March 3, 2016 20:44
Show Gist options
  • Save Carlsson87/245b226a955a750d03fd to your computer and use it in GitHub Desktop.
Save Carlsson87/245b226a955a750d03fd to your computer and use it in GitHub Desktop.
PHP + NodeJS + Socket.io
use GuzzleHttp\Client;
$client = new Client([
'base_uri' => '127.0.0.1:8001',
]);
$client->request('POST', 'http://httpbin.org/post', [
'body' => 'raw data'
]);
var socket = io('http://127.0.0.1:8001');
socket.emit('joining', 'a_room');
socket.on('message', function (data) {
console.log(data);
});
send = function(message) {
socket.emit('message', message);
}
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
app.listen(8001);
io.on('connection', function (socket) {
socket.on('joining', function(room) {
socket.room = room;
socket.join(room);
});
socket.on('message', function(message) {
socket.broadcast.to(room).emit('message', message);
});
});
function handler (req, res) {
req.on('data', function(chunk) {
io.sockets.in('a_room').emit('message', JSON.parse(chunk.toString()));
});
res.writeHead(200);
res.end("Message sent");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment