Skip to content

Commit bf5cecd

Browse files
author
Sergio Daniel Xalambrí
committed
[add] v1.0.0
0 parents  commit bf5cecd

File tree

8 files changed

+163
-0
lines changed

8 files changed

+163
-0
lines changed

.babelrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"plugins": [
3+
"transform-es2015-modules-commonjs",
4+
"transform-class-properties",
5+
"transform-es2015-spread",
6+
"transform-es2015-classes",
7+
"transform-react-constant-elements",
8+
"transform-react-inline-elements"
9+
]
10+
}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
build
3+
log
4+
*.log

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
test
3+
lib
4+
.babelrc

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# socket.io-react
2+
A High-Order component to connect React and Socket.io easily.
3+
4+
## API
5+
```javascript
6+
import {
7+
SocketProvider,
8+
socketConnect,
9+
} from 'socket.io-react';
10+
```
11+
12+
### SocketProvider(socket?)
13+
```javascript
14+
import { SocketProvider } from 'socket.io-react';
15+
import io from 'socket.io-client';
16+
17+
import App from './containers/App';
18+
19+
const socket = io.connect(process.env.SOCKET_URL);
20+
socket.on('message', msg => console.log(msg));
21+
22+
const DOMNode = document.getElementById('renderTarget');
23+
24+
render(
25+
<SocketProvider socket={socket}>
26+
<App />
27+
</SocketProvider>,
28+
DOMNode
29+
);
30+
```
31+
* `socket` property is `false` by default.
32+
33+
### socketConnect(Target)
34+
```javascript
35+
import { socketConnect } from 'socket.io-react';
36+
37+
function App(props) {
38+
function sendMessage() {
39+
props.socket.emit('message', 'Hello world!');
40+
}
41+
42+
return (
43+
<button onClick={sendMessage}>
44+
Send!
45+
</button>
46+
);
47+
}
48+
49+
export default socketConnect(App);
50+
```
51+
* `socketConnect` can be used as a decorator (using [**babel-plugin-transform-decorators-legacy**](https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy))

lib/SocketProvider.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React, { Component, PropTypes} from 'react';
2+
3+
class SocketProvider extends Component {
4+
static propTypes = {
5+
socket: PropTypes.oneOfType([
6+
PropTypes.bool, PropTypes.object
7+
]),
8+
};
9+
10+
static defaultProps = {
11+
socket: false,
12+
};
13+
14+
static childContextTypes = {
15+
socket: PropTypes.oneOfType([
16+
PropTypes.bool, PropTypes.object
17+
]),
18+
};
19+
20+
getChildContext() {
21+
return {
22+
socket: this.props.socket,
23+
};
24+
}
25+
26+
render() {
27+
return this.props.children;
28+
}
29+
}
30+
31+
export default SocketProvider;

lib/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import SocketProvider from './SocketProvider.js';
2+
import socketConnect from './socket-connect.js';
3+
4+
export default {
5+
SocketProvider,
6+
socketConnect,
7+
};

lib/socket-connect.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React, { createFactory, PropTypes } from 'react';
2+
3+
function socketConnect(Target) {
4+
function SocketConnect(props, context) {
5+
props.socket = context.socket;
6+
7+
return createFactory(Target)(props);
8+
}
9+
10+
SocketConnect.contextTypes = {
11+
socket: PropTypes.oneOfType([
12+
PropTypes.bool, PropTypes.object
13+
]),
14+
}
15+
16+
return SocketConnect;
17+
}
18+
19+
export default socketConnect;

package.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "socket.io-react",
3+
"version": "1.0.0",
4+
"description": "A High-Order Component to connect React and Socket.io",
5+
"main": "build/index.js",
6+
"scripts": {
7+
"build": "babel lib --out-dir build"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/sergiodxa/socket.io-react.git"
12+
},
13+
"keywords": [
14+
"react",
15+
"socket.io",
16+
"websocket"
17+
],
18+
"author": "Sergio Daniel Xalambrí <[email protected]> (http://sergio.xalambri.com.ar/)",
19+
"license": "MIT",
20+
"bugs": {
21+
"url": "https://github.com/sergiodxa/socket.io-react/issues"
22+
},
23+
"homepage": "https://github.com/sergiodxa/socket.io-react#readme",
24+
"devDependencies": {
25+
"babel": "6.5.2",
26+
"babel-cli": "6.6.5",
27+
"babel-plugin-transform-class-properties": "6.6.0",
28+
"babel-plugin-transform-es2015-classes": "6.6.5",
29+
"babel-plugin-transform-es2015-modules-commonjs": "6.7.0",
30+
"babel-plugin-transform-es2015-spread": "6.6.5",
31+
"babel-plugin-transform-react-constant-elements": "6.5.0",
32+
"babel-plugin-transform-react-inline-elements": "6.6.5"
33+
},
34+
"peerDependencies": {
35+
"socket.io-client": "^1.4.5"
36+
}
37+
}

0 commit comments

Comments
 (0)