Skip to content

Commit 2f79197

Browse files
committed
added react and flask socket
1 parent 06eb886 commit 2f79197

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [Select Single Checkbox](js/single-checkbox-selection.js)
88
- [Datetime (Unix , RealTime and Formatted Datetime)](js/datetime.js)
99
## React JS
10+
- [React Socket Connection](react-js/Socket.js)
1011
- [Exportable API Endpoint](react-js/api/ApiHelper.js)
1112
- [Example API Helper Routes ](react-js/api/ExampleApi.js)
1213
- [API Integration Add Component](react-js/api/ApiAdd.js)
@@ -27,7 +28,7 @@
2728

2829
## Python
2930
- [Find Router Connected Ip List of](python/router_ip_address.py)
30-
31+
- [Flask Socket Connection](python/flask_socket.py)
3132
- [Google Cloud Pub/Sub Publishing/subscription messages and subscriptions list](python/pubsub.py)
3233
- [Dockerize Flask App](python/Dockerfile)
3334
- [Cython Compile File Generator](python/compile.py)

python/flask_socket.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# pip install flask flask_socketio
2+
from flask import Flask
3+
from flask_socketio import SocketIO,emit
4+
import flask_socketio
5+
6+
app = Flask(__name__)
7+
socketio = SocketIO(app)
8+
9+
10+
@socketio.on('connect')
11+
def test_message():
12+
data = "asd"
13+
emit('my', {data: 'got it!'})
14+
15+
if __name__ == '__main__':
16+
# app.run(debug=True)
17+
socketio.run(app, debug=True)
18+

react-js/Socket.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// npm install socket.io-client
2+
import React,{Component} from 'react';
3+
import socketIOClient from "socket.io-client"
4+
5+
class Socket extends Component {
6+
7+
constructor() {
8+
super();
9+
this.state = {
10+
response: 0,
11+
endpoint: "http://localhost:5000"
12+
};
13+
}
14+
15+
componentDidMount() {
16+
17+
const {endpoint} = this.state;
18+
const socket = socketIOClient(endpoint);
19+
console.log("MOUNT");
20+
socket.on("my", function (data) {
21+
console.log("GETTING DATA ", data);
22+
})
23+
}
24+
25+
render() {
26+
27+
return (
28+
<div>
29+
Test Socket
30+
</div>
31+
)
32+
}
33+
}
34+
export default Socket;

0 commit comments

Comments
 (0)