Skip to content

Commit e01c593

Browse files
committed
Configure web socket connection to Phoenix channel
1 parent f3b1bdd commit e01c593

File tree

6 files changed

+41
-2
lines changed

6 files changed

+41
-2
lines changed

lib/phoenix_react_redux_example/endpoint.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule PhoenixReactReduxExample.Endpoint do
22
use Phoenix.Endpoint, otp_app: :phoenix_react_redux_example
33

4-
socket "/socket", PhoenixReactReduxExample.UserSocket
4+
socket "/ws", PhoenixReactReduxExample.UserSocket
55

66
# Serve at "/" the static files from "priv/static" directory.
77
#

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"babel-loader": "^6.0.1",
3232
"babel-preset-es2015": "^6.1.2",
3333
"babel-preset-react": "^6.1.2",
34+
"copy-webpack-plugin": "^0.2.0",
3435
"css-loader": "^0.22.0",
3536
"extract-text-webpack-plugin": "^0.9.1",
3637
"node-libs-browser": "^0.5.3",

web/static/js/actions.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ function addTodoFailure(text) {
3838
export function addTodo(text) {
3939
return dispatch => {
4040
dispatch(addTodoRequest(text));
41+
42+
// add todo, then dispatch success/failure
4143
dispatch(addTodoSuccess(text));
44+
dispatch(addTodoFailure(text));
4245
};
4346
}
4447

web/static/js/channel.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Socket } from 'phoenix';
2+
3+
export function configureChannel() {
4+
let socket = new Socket('/ws');
5+
socket.connect();
6+
7+
let channel = socket.channel('todos');
8+
9+
channel.on('new:todo', msg => console.log('new:todo', msg));
10+
11+
channel.join()
12+
.receive('ok', messages => console.log('catching up', messages))
13+
.receive('error', reason => console.log('failed join', reason))
14+
.after(10000, () => console.log('Networking issue. Still waiting...'));
15+
}

web/static/js/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { createStore, applyMiddleware } from 'redux';
66
import { Provider } from 'react-redux';
77
import App from './containers/App';
88
import todoApp from './reducers';
9+
import { configureChannel } from './channel';
910

1011
const loggerMiddleware = createLogger();
1112

@@ -16,6 +17,8 @@ const createStoreWithMiddleware = applyMiddleware(
1617

1718
const store = createStoreWithMiddleware(todoApp);
1819

20+
configureChannel(store);
21+
1922
render(
2023
<Provider store={store}>
2124
<App />

webpack.config.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
var webpack = require('webpack');
22
var path = require('path');
33
var ExtractTextPlugin = require('extract-text-webpack-plugin');
4+
var CopyWebpackPlugin = require('copy-webpack-plugin');
45

56
var env = process.env.MIX_ENV || 'dev';
67
var isProduction = (env === 'prod');
78

8-
var plugins = [new ExtractTextPlugin('app.css')];
9+
var plugins = [
10+
new ExtractTextPlugin('app.css'),
11+
new CopyWebpackPlugin([
12+
{ from: './web/static/assets' },
13+
{ from: './deps/phoenix_html/web/static/js/phoenix_html.js',
14+
to: 'js/phoenix_html.js' }
15+
])
16+
];
917

1018
// This is necessary to get the sass @import's working
1119
var stylePathResolves = (
@@ -19,10 +27,18 @@ if (isProduction) {
1927

2028
module.exports = {
2129
entry: './web/static/js/index.js',
30+
2231
output: {
2332
path: './priv/static/js',
2433
filename: 'app.js'
2534
},
35+
36+
resolve: {
37+
alias: {
38+
phoenix: __dirname + '/deps/phoenix/web/static/js/phoenix.js'
39+
}
40+
},
41+
2642
module: {
2743
loaders: [
2844
{
@@ -43,5 +59,6 @@ module.exports = {
4359

4460
]
4561
},
62+
4663
plugins: plugins
4764
};

0 commit comments

Comments
 (0)