https://github.com/diloabininyeri/rest-socket
Send socket notification on HTTP rest API
https://github.com/diloabininyeri/rest-socket
php7 rest-api socket-io
Last synced: 3 months ago
JSON representation
Send socket notification on HTTP rest API
- Host: GitHub
- URL: https://github.com/diloabininyeri/rest-socket
- Owner: diloabininyeri
- Created: 2021-07-07T08:30:28.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-08-02T09:43:48.000Z (over 3 years ago)
- Last Synced: 2025-03-26T16:11:32.307Z (10 months ago)
- Topics: php7, rest-api, socket-io
- Language: JavaScript
- Homepage:
- Size: 1.65 MB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# socket.io rest
send to message or data with any software langauge,no need more enviroument just use http rest api
get from docker hub...
```console
docker run --name rest-socket -p 3000:3000 diloabininyeri/rest-socket
```
and see it
```
http://localhost:3000
```
with js example code
```js
function socket_emit(channel, json) {
let query = new URLSearchParams(json).toString();
fetch(`http://172.16.45.131:3000/${channel}/?token=${token}`, {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: query
}).then(a => a.json()).then(e => console.log(e));
}
socket_emit('notify', {seen: 45})
socket.on("notify", (a) => console.log(a))
```
Using on laravel
```php
use Illuminate\Support\Facades\Http;
$url = 'http://localhost:3000/chanel_name?token=$2y$10$.5iuqFaSaMQrPi/rMmUVjOJg/Ip6gEI5Jzhux.tzfyUu2ZmPOAs2C';
$payload = ['name' => 'Dılo sürücü'];
$response = Http::asForm()
->post(
$url,
$payload
);
return $response->body();
```
send data to socket with php
```php
$url = "http://localhost:3000/chanelname?token=$2y$10$.5iuqFaSaMQrPi/rMmUVjOJg/Ip6gEI5Jzhux.tzfyUu2ZmPOAs2C";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"name=dılo&surname=sürücü");
.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
```
example subscribe to socket in html via js
```js
const token = "$2y$10$.5iuqFaSaMQrPi/rMmUVjOJg/Ip6gEI5Jzhux.tzfyUu2ZmPOAs2C";
const uri = "yoursocketserver.com:3000";
let socket = io(uri, {auth: {token: token}});
function socket_emit(channel, json) {
let query = new URLSearchParams(json).toString();
fetch(`http://172.16.45.131:3000/${channel}/?token=${token}`, {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: query
}).then(a => a.json()).then(e => console.log(e));
}
socket.on('connect_error', (error) => console.error(error));
socket.on("my-channel",(a)=>alert(JSON.stringify(a)))
socket_emit("my-channel",{name:"dılo"});
```
Test with curl
```console
curl --location --request GET 'http://localhost:3000/chanel_name?token=$2y$10$/GluV3g/0wcaU5A301pd0O1EeucaOrZym2Yh9CCti6NwROwnk4vba' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'name=Dılo sürücü'
```
