Giter Site home page Giter Site logo

socket.io-php-emitter's Introduction

socket.io-php-emitter

A PHP implementation of socket.io-emitter.

This project requires a Redis client for PHP. If you dont have the PECL Redis installed, the emitter will default to using TinyRedisClient. You can, however, pass in any Redis client that supports a publish method.

Installation and development

To install and use in your PHP project, install it as a composer package. Install dependencies with composer install.

To run tests, invoke make test. The current test suite will just be checking redis monitor that everything is published correctly. Some work will be put into making a better integration test suite in the near future.

Usage

Initialization

$redis = new \Redis(); // Using the Redis extension provided client
$redis->connect('127.0.0.1', '6379');
$emitter = new SocketIO\Emitter($redis);
$emitter->emit('event', 'payload str');

Broadcasting and other flags

Possible flags

  • json
  • volatile
  • broadcast
// Below initialization will create a  phpredis client, or a TinyRedisClient depending on what is installed
$emitter = new SocketIO\Emitter(array('port' => '6379', 'host' => '127.0.0.1'));
// broadcast can be replaced by any of the other flags
$emitter->broadcast->emit('other event', 'such data');

Emitting objects

$emitter = new SocketIO\Emitter(); // If arguments are not provided, they will default to array('port' => '6379', 'host' => '127.0.0.1')
$emitter->emit('event', array('property' => 'much value', 'another' => 'very object'));

socket.io-php-emitter's People

Contributors

kitek avatar marcguyer avatar rase- avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

socket.io-php-emitter's Issues

Does this work anymore?

I used it maybe half a year ago and recently started playing around with it again. For some reason it stopped working so I stripped it down to the follow:

<?php

require_once( 'socketio/src/Emitter.php' );

$emitter = new SocketIO\Emitter( array( 'port' => '6379', 'host' => '127.0.0.1') );

$emitter->broadcast->emit( 'testcall', 'data' );

?>

1492711935.962165 [0 127.0.0.1:62109] "PUBLISH" "socket.io#/#" "\x93\xa7emitter\x83\xa4type\x02\xa4data\x91\xa8testcall\xa3nsp\xa1/\x82\xa5rooms\x90\xa5flags\x81\xa9broadcast\xc3"

{
    "name": "Emitter",
    "devDependencies": {
        "express": "~4.15.2",
        "socket.io": "~1.7.3",
        "socket.io-redis": "~4.0.0"
    }
}
var app = require( 'express' )( ),
    http = require( 'http' ).Server( app ),
    io = require('socket.io')( http ),
    redis = require( 'socket.io-redis' );

http.listen( 8081, function( ) {
    console.log( '\n==========================' );
    console.log( 'server listening on *:8081' );
    console.log( '==========================\n' );
});

io.adapter( redis({ host: '127.0.0.1', port: 6379 }) );

io.on( 'connection', function( socket ) {
    socket.on( 'testcall', function( ) {
        console.log( 'testcall' );
    });
});

I have redis-cli monitor open and I can see it publishing data to redis but I never see the log appear. I can emit events from a browser but the PHP ones never appear.

Flags is not work

$emitter->broadcast->emit('event', 'payload str');

the broadcast flag is ignored, same with the other flags.

read error on connection

Hi,

I get error "read error on connection" when I emit data. The connection seems to be ok though. When I create an emitter object, I get a connected object (please see below)

$redis = new \Redis();
$redis->connect($_SERVER['SERVER_ADDR'], '8090');
$emitter = new \SocketIO\Emitter($redis);
dd($emitter);

Emitter {#514 ▼
+"redis": Redis {#519 ▼
isConnected: true
host: "172.16.6.66"
port: 8090
auth: null
dbNum: 0
timeout: 0.0
persistentId: null
options: {▼
READ_TIMEOUT: 0.0
SERIALIZER: NONE
PREFIX: null
SCAN: NORETRY
}
}
+"key": "socket.io#emitter"
+"_rooms": []
+"_flags": []
}

However, whenever I try to emit data, I get errors.

$emitter->broadcast->emit('event', array('name' => 'foo'));

Make binary work

Msgpack-js has some extensions to it, which means that the PHP implementation of msgpack isn't totally compatible for emitting binary, since it's not decoded into buffers in the receiving end. Hack your way to using those extensions to make msgpack-js decode into buffers.

Cannot get server get the socket

I know php emmiter is emiting the sockets, but server is not getting them.

This is my php code

$redis = new \Redis();
            $redis->connect('127.0.0.1', '6379');
            $emitter = new SocketIO\Emitter($redis);
            $emitter->broadcast->emit('haha', 'such data');

This is my server code

var io = require('socket.io')(3000);
var redis = require('socket.io-redis');
io.adapter(redis({ host: '127.0.0.1', port: 6379 }));
io.on('connection', function(socket){
    console.log(socket + " connected");
    socket.on('haha', function(data){
        console.log("data " + data);
    });
    socket.on('disconnect', function(){
        console.log(socket + " disconnected");
    });
});

redis-cli monitor output:

1466856693.275138 [0 127.0.0.1:34296] "info"
1466856693.276242 [0 127.0.0.1:34298] "info"
1466856693.284131 [0 127.0.0.1:34298] "subscribe" "socket.io#/#" //nodejs server start
1466856702.934495 [0 127.0.0.1:34332] "PUBLISH" "socket.io#/#" "\x93\xa7emitter\x83\xa4type\x02\xa4data\x92\xa4haha\xa9such data\xa3nsp\xa1/\x82\xa5rooms\x90\xa5flags\x81\xa9broadcast\xc3" //php code exec

Im running Ubuntu Server 16.04 with php7, node 4.4.6, redis-server 3.0.6. Any ideas why it doesnt work? Thanks a lot!

Make ptrofimov/tinyredisclient optional

This is an optional dependency, which should not be required by composer. A class_exists check should be implemented, and the lib should only try to use this client if it's available. If no redis implementation is usable, throw an exception.

HTTPS

Does it work with https transport? I am getting this error "stream_socket_client(): unable to connect to xxxxxxxxxx:pppp (Connection refused)"

Not sure what I'm doing?

I'm running Ubuntu 15.04 Apache and I'm trying this out for the first time, I've managed to use composer to install the emitter, PHP Redis appears to be working correctly as well.

I made a test code to see if everything runs smoothly, so I started listening to my index.js (opened socket 3000) and I ran my test file which includes the following:

<?php
require 'vendor/autoload.php';
require ('vendor/rase/socket.io-emitter/src/Emitter.php');
$emitter = new SocketIO\Emitter(array('port' => '6379', 'host' => '127.0.0.1'));
$emitter->broadcast->emit('chat message', 'such data');
?>

I know that PHP redis is working because when I run the file I get this message in my redis-cli monitor:

1441747867.750559 [0 127.0.0.1:52963] "PUBLISH" "socket.io#emitter" "\x92\x83\xa4type\x02\xa4data\x92\xacchat message\xa9such data\xa3nsp\xa1/\x82\xa5rooms\x90\xa5flags\x81\xa9broadcast\xc3"

However, I don't see a response being emitted on socket.io, what could I possibly be doing wrong?

what does line 103 do ?

as it is:

$this->binary;

there is no method or member variable named binary in class Emitter, so what is it , maybe it's a mistake?

Php script does not find class Redis

When I run the script in the example I get an error by which class Redis (whether or not preceeded by ) is not found. I installed the whole by composer but the example misses any include in the file. How may I have the script find the Redis class?

not recieving any events in node, why?

Im trying to get this to work on my localhost.

on node:

    //we create a separate pub and sub so that we can specify authentication
    var ioPub = redis.createClient(settings.redisPort, settings.redisHost, {auth_pass: settings.redisPassword});
    var ioSub = redis.createClient(settings.redisPort, settings.redisHost, {return_buffers: true, auth_pass: settings.redisPassword});
    var adapter = redisio({pubClient: ioPub, subClient: ioSub});
    this.io = require('socket.io')(server.getHttpServer(), {enablesXDR: true});
    this.io.adapter(adapter)

settings are: localhost and port 6379, password = false in my local environment.

in PHP

   $emitter = new \SocketIO\Emitter(); // If arguments are not provided, they will default to array('port' => '6379', 'host' => '127.0.0.1')
   $emitter->emit($eventName,$data);

Im seeing action in the redis window, but I dont recieve any events in node. Any idea why?

did not get anything in my backend

I can not get any response in my backend

Here is mycode

`require_once DIR . '/../vendor/autoload.php'; // Autoload files using Composer autoload
include 'process.php';

use SocketIO\Emitter;

$emitter = new SocketIO\Emitter(array('port' => '5037', 'host' => 'myhost'));
// broadcast can be replaced by any of the other flags
$json['chat']="ded27825e3-0hus5w4vkg1h-664";
$json['message']="Form php";
$json['type']=1;
$json['toBotId']=538;
// $json = json_encode($json, JSON_UNESCAPED_UNICODE);
$emitter->broadcast->emit('sendChatNew', array('chat' => 'ded27825e3-hq7v4dxgsho-295', 'message' => 'very object','type' =>1,'toBotId' =>538));`

Sending token (for JWT) to socket.io server

Is it possible to add a query parameter when connecting to a socket.io server, so that client can be authenticated (via JWT)?

In a javascript client, it is as easy as:

var socket = io.connect('http://localhost:9000', {
  'query': 'token=' + your_jwt
});

Message pack improperly serializes maps with numerical indexes

When emitting a map of data that uses non-sequential numerical indexes, e.g.:

$data = [
    "3" => "foo",
    "7" => "bar"
];
$emitter = new SocketIO\Emitter(); 
$emitter->emit('event', $data);

The data is serialized, emitted, and received as an array, e.g.:

{
    "0": "foo",
    "1": "bar"
}

The only way around this right now is to add a non-numerical key into the map, e.g.:

$data = [
    "3" => "foo",
    "7" => "bar",
    "hack" => true
];
$emitter->emit('event', $data);

The reason this is happening is because in this file (https://github.com/rase-/socket.io-php-emitter/blob/master/src/msgpack_pack.php#L79) starting at line 79, the code is converting the data to an array if all keys in the data are ints, regardless of if the keys are sequential or not.

Decouple Redis from the emitter

I've forked your project and I'll try to decouple Redis from the emitter.
I personally won't use redis to send events to nodejs server.

I'll let you know how I go but I'll probably start from scratch :)

Check if room exists/if anyone is in room

Is there a way to check whether or not a room exists before using emitter->to(room)->emit()? If there's no one in room then i'd like PHP to execute a different script, is there any way to handle this??

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.