Skip to content

Commit 1f834f2

Browse files
committedJun 18, 2017
Initial
0 parents  commit 1f834f2

File tree

9 files changed

+229
-0
lines changed

9 files changed

+229
-0
lines changed
 

‎bin/config.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"server_port":8080,
3+
"channels":["admin","user"]
4+
}

‎bin/package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "resonance",
3+
"version": "1.0.0",
4+
"description": "An agnostic PHP library for realtime",
5+
"main": "server.js",
6+
"scripts": {
7+
"start": "node server.js"
8+
},
9+
"author": "Igor C. de Paula <igordepaula@adminweb.com.br>",
10+
"license": "ISC",
11+
"dependencies": {
12+
"redis": "^2.7.1",
13+
"socket.io": "^2.0.3"
14+
}
15+
}

‎bin/server.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var config = require('./config.json');
2+
var io = require('socket.io')(config.server_port)
3+
var redis = require('redis')
4+
5+
var sub = redis.createClient();
6+
7+
var chans = config.channels.map(function(chan){
8+
var chan = io.of('/'+chan)
9+
return chan
10+
})
11+
12+
sub.on("message",function(channel, data){
13+
var message = JSON.parse(data)
14+
chans.map(function(chan){
15+
chan.emit(message.event, message.message)
16+
})
17+
});
18+
config.channels.map(function(chan){
19+
sub.subscribe(chan)
20+
})
21+

‎composer.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "adminweb/resonance",
3+
"description": "A PHP library for agnostic realtime",
4+
"minimum-stability": "stable",
5+
"license": "MIT",
6+
"version":"1.0.0",
7+
"authors": [
8+
{
9+
"name": "Igor C. de Paula",
10+
"email": "igordepaula@adminweb.com.br"
11+
}
12+
],
13+
"require": {
14+
"predis/predis": "^1.1"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"Resonance\\": "src/Resonance/"
19+
}
20+
},
21+
"bin":[
22+
"bin/config.json",
23+
"bin/package.json",
24+
"bin/server.js"
25+
]
26+
}

‎examples/index.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html>
2+
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
3+
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
4+
<script>
5+
var admin = io.connect('http://localhost:8080/admin');
6+
7+
admin.on('Soar',function(data){
8+
console.log('conectado')
9+
console.log(data)
10+
})
11+
12+
</script>
13+
</html>
14+

‎examples/second.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
require '../vendor/autoload.php';
3+
4+
use Resonance\Bell;
5+
use Resonance\BeatInterface;
6+
7+
class Soar implements BeatInterface{
8+
9+
/**
10+
* @return mixed
11+
*/
12+
public function getChannel()
13+
{
14+
return 'admin';
15+
}
16+
17+
/**
18+
* @return mixed
19+
*/
20+
public function getMusic()
21+
{
22+
return 'Eu to aqui';
23+
}
24+
25+
public function getEvent()
26+
{
27+
return 'Soar';
28+
}
29+
}
30+
31+
$bell = new Bell();
32+
33+
$bell->touch(new Soar());

‎readme.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Resonance
2+
=========
3+
4+
An agnostic framework PHP library for realtime.
5+
6+
Requirements
7+
------------------
8+
PHP >= 5.5
9+
Redis Server
10+
NodeJS >= 6
11+
12+
13+
Usage
14+
--------
15+
Install dependencies on ```/vendor/bin/package.json```.
16+
17+
Configure port and channels on ```/vendor/bin/config.json```.
18+
19+
Star the server in background with [forever](https://www.npmjs.com/package/forever) or [PM2](https://www.npmjs.com/package/pm2).
20+
21+
Implements the ```Resonance\BeatInterface``` on your classes on your app.
22+
23+
Create a new Bell instance for use touch (method) your beat (implements BeatInterface) (like example folder).
24+
25+
On Front
26+
------------
27+
Put a tg script with src attribute with value ```http://server-address:server-port/socket.io/socket.io.js```.
28+
29+
Initialize the connection with
30+
31+
```javascript
32+
var conn = io.connect('http://server-address:server-port/your-channel');
33+
conn.on('your-event',function(music){
34+
console.log('your music', music)
35+
});
36+
```
37+
Be happy =D
38+
39+
40+
41+

‎src/Resonance/BeatInterface.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: igor
5+
* Date: 17/06/17
6+
* Time: 13:15
7+
*/
8+
9+
namespace Resonance;
10+
11+
12+
/**
13+
* Interface BeatInterface
14+
* @package Resonance
15+
*/
16+
interface BeatInterface
17+
{
18+
/**
19+
* Channel for publish the music
20+
* @return mixed
21+
*/
22+
public function getChannel();
23+
24+
/**
25+
* Event for pusblish the music
26+
* @return mixed
27+
*/
28+
public function getEvent();
29+
30+
/**
31+
* The music
32+
* @return mixed
33+
*/
34+
public function getMusic();
35+
}
36+

‎src/Resonance/Bell.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: igor
5+
* Date: 17/06/17
6+
* Time: 13:15
7+
*/
8+
9+
namespace Resonance;
10+
11+
12+
use Predis\Client;
13+
14+
/**
15+
* Class Bell
16+
* @package Resonance
17+
*/
18+
class Bell
19+
{
20+
private $client = null;
21+
22+
/**
23+
* Bell constructor.
24+
*/
25+
public function __construct()
26+
{
27+
$this->client = new Client();
28+
}
29+
30+
/**
31+
* @param BeatInterface $beat
32+
*/
33+
public function touch(BeatInterface $beat){
34+
$band['event'] = $beat->getEvent();
35+
$band['music'] = $beat->getMusic();
36+
$this->client->publish($beat->getChannel(), json_encode($band));
37+
}
38+
}
39+

0 commit comments

Comments
 (0)
Please sign in to comment.