Skip to content

Commit ccea1f3

Browse files
committed
feat(gen): replace socket.io w/ primus + uws
#2565
1 parent 79bdeed commit ccea1f3

File tree

21 files changed

+164
-136
lines changed

21 files changed

+164
-136
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ module.exports = function (grunt) {
112112
testing: 'jasmine',
113113
auth: true,
114114
oauth: ['googleAuth', 'twitterAuth'],
115-
socketio: true
115+
ws: true
116116
};
117117

118118
var deps = [

docs/01_Getting_Started/04_Project_Overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ webpack.make.js // main file for Webpack configuration
111111
│ local.env.js // ignored by Git
112112
│ local.env.sample.js // sensitive environment variables are stored here, and added at server start. Copy to `local.env.js`.
113113
│ seed.js // re-seeds database with fresh data
114-
socketio.js // Socket IO configuration / imports
114+
websockets.js // WebSocket configuration / imports
115115
116116
└───environment
117117
development.js

src/generators/app/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,14 @@ export class Generator extends Base {
270270
}]
271271
}, {
272272
type: 'confirm',
273-
name: 'socketio',
274-
message: 'Would you like to use socket.io?',
273+
name: 'ws',
274+
message: 'Would you like to use WebSockets?',
275275
// to-do: should not be dependent on ODMs
276276
when: answers => answers.odms && answers.odms.length !== 0,
277277
default: true
278278
}]).then(answers => {
279-
if(answers.socketio) this.filters.socketio = true;
280-
insight.track('socketio', !!answers.socketio);
279+
if(answers.ws) this.filters.ws = true;
280+
insight.track('ws', !!answers.ws);
281281

282282
if(answers.auth) this.filters.auth = true;
283283
insight.track('auth', !!answers.auth);
@@ -374,7 +374,7 @@ export class Generator extends Base {
374374
this.config.set('pluralizeRoutes', true);
375375

376376
this.config.set('insertSockets', true);
377-
this.config.set('registerSocketsFile', 'server/config/socketio.js');
377+
this.config.set('registerSocketsFile', 'server/config/websockets.js');
378378
this.config.set('socketsNeedle', '// Insert sockets below');
379379

380380
this.config.set('insertModels', true);

src/generators/endpoint/index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class Generator extends NamedBase {
9797
end() {
9898
if(this.config.get('insertRoutes')) {
9999
var routesFile = this.config.get('registerRoutesFile');
100-
var reqPath = this.relativeRequire(this.routeDest, routesFile);
100+
let reqPath = this.relativeRequire(this.routeDest, routesFile);
101101
var routeConfig = {
102102
file: routesFile,
103103
needle: this.config.get('routesNeedle'),
@@ -108,23 +108,22 @@ export class Generator extends NamedBase {
108108
this.rewriteFile(routeConfig);
109109
}
110110

111-
if(this.filters.socketio && this.config.get('insertSockets')) {
111+
if(this.filters.ws && this.config.get('insertSockets')) {
112112
var socketsFile = this.config.get('registerSocketsFile');
113-
var reqPath = this.relativeRequire(this.routeDest + '/' + this.basename +
114-
'.socket', socketsFile);
113+
let reqPath = this.relativeRequire(this.routeDest + '/' + this.basename + '.socket', socketsFile);
115114
var socketConfig = {
116115
file: socketsFile,
117116
needle: this.config.get('socketsNeedle'),
118117
splicable: [
119-
`require('${reqPath}').register(socket);`
118+
`require('${reqPath}').register,`
120119
]
121120
};
122121
this.rewriteFile(socketConfig);
123122
}
124123

125124
if(this.filters.sequelize && this.config.get('insertModels')) {
126125
var modelsFile = this.config.get('registerModelsFile');
127-
var reqPath = this.relativeRequire(`${this.routeDest}/${this.basename}.model`, modelsFile);
126+
let reqPath = this.relativeRequire(`${this.routeDest}/${this.basename}.model`, modelsFile);
128127
var modelConfig = {
129128
file: modelsFile,
130129
needle: this.config.get('modelsNeedle'),

src/test/endpoint.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const defaultOptions = {
3030
odms: ['mongoose'],
3131
auth: true,
3232
oauth: [],
33-
socketio: true
33+
ws: true
3434
};
3535

3636
function runEndpointGen(name, opt={}) {

src/test/fixtures/.yo-rc.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"routesBase": "/api/",
88
"pluralizeRoutes": true,
99
"insertSockets": true,
10-
"registerSocketsFile": "server/config/socketio.js",
10+
"registerSocketsFile": "server/config/websockets.js",
1111
"socketsNeedle": "// Insert sockets below",
1212
"insertModels": true,
1313
"registerModelsFile": "server/sqldb/index.js",
@@ -21,7 +21,7 @@
2121
"uirouter": true,
2222
"bootstrap": true,
2323
"uibootstrap": true,
24-
"socketio": true,
24+
"ws": true,
2525
"auth": true,
2626
"models": true,
2727
"mongooseModels": true,

src/test/get-expected-files.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,18 @@ export function app(options) {
203203
'client/components/oauth-buttons/oauth-buttons.' + stylesheet,
204204
'client/components/oauth-buttons/oauth-buttons.' + markup,
205205
'client/components/oauth-buttons/oauth-buttons.component.' + script,
206-
'client/components/oauth-buttons/oauth-buttons.component.spec.' + script,
206+
'client/components/oauth-buttons/oauth-buttons.component.spec.' + script,
207207
'e2e/components/oauth-buttons/oauth-buttons.po.js'
208208
]);
209209
}
210210

211-
/* Socket.IO */
212-
if (options.socketio) {
211+
/* WebSockets */
212+
if (options.ws) {
213213
files = files.concat([
214214
'client/components/socket/socket.service.' + script,
215215
'client/components/socket/socket.mock.' + script,
216216
'server/api/thing/thing.socket.js',
217-
'server/config/socketio.js'
217+
'server/config/websockets.js'
218218
]);
219219
}
220220

src/test/main.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const defaultOptions = {
2727
odms: ['mongoose'],
2828
auth: true,
2929
oauth: [],
30-
socketio: true
30+
ws: true
3131
};
3232
const TEST_DIR = __dirname;
3333

@@ -198,7 +198,7 @@ describe('angular-fullstack:app', function() {
198198
odms: ['mongoose'],
199199
auth: true,
200200
oauth: ['twitterAuth', 'facebookAuth', 'googleAuth'],
201-
socketio: true,
201+
ws: true,
202202
bootstrap: true,
203203
uibootstrap: true
204204
};
@@ -270,7 +270,7 @@ describe('angular-fullstack:app', function() {
270270
odms: ['sequelize'],
271271
auth: true,
272272
oauth: ['twitterAuth', 'facebookAuth', 'googleAuth'],
273-
socketio: true,
273+
ws: true,
274274
bootstrap: true,
275275
uibootstrap: true
276276
};
@@ -343,7 +343,7 @@ describe('angular-fullstack:app', function() {
343343
odms: [],
344344
auth: false,
345345
oauth: [],
346-
socketio: false,
346+
ws: false,
347347
bootstrap: false,
348348
uibootstrap: false
349349
};

templates/app/_package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141
"passport-local": "^1.0.0",<% } %><% if(filters.facebookAuth) { %>
4242
"passport-facebook": "^2.0.0",<% } %><% if(filters.twitterAuth) { %>
4343
"passport-twitter": "^1.0.3",<% } %><% if(filters.googleAuth) { %>
44-
"passport-google-oauth20": "^1.0.0",<% } %><% if(filters.socketio) { %>
45-
"socket.io": "^1.3.5",
46-
"socket.io-client": "^1.3.5",
47-
"socketio-jwt": "^4.2.0",<% } %>
44+
"passport-google-oauth20": "^1.0.0",<% } %><% if(filters.ws) { %>
45+
"primus": "^7.0.1",
46+
"primus-emit": "^1.0.0",
47+
"uws": "^0.14.5",<% } %>
4848
"serve-favicon": "^2.3.0",
4949
"shrink-ray": "^0.1.3"
5050
},

templates/app/client/app/main/main.component.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, OnInit<% if(filters.socketio) { %>, OnDestroy<% } %> } from '@angular/core';
1+
import { Component, OnInit<% if(filters.ws) { %>, OnDestroy<% } %> } from '@angular/core';
22
import { Http } from '@angular/http';
33
import { Observable } from 'rxjs/Observable';
44
import { SocketService } from '../../components/socket/socket.service';
@@ -8,34 +8,34 @@ import { SocketService } from '../../components/socket/socket.service';
88
template: require('./main.<%=templateExt%>'),
99
styles: [require('./main.<%=styleExt%>')],
1010
})
11-
export class MainComponent implements OnInit<% if(filters.socketio) { %>, OnDestroy<% } %> {
12-
<%_ if(filters.socketio) { -%>
11+
export class MainComponent implements OnInit<% if(filters.ws) { %>, OnDestroy<% } %> {
12+
<%_ if(filters.ws) { -%>
1313
SocketService;<% } %>
1414
awesomeThings = [];
1515
<%_ if(filters.models) { -%>
1616
newThing = '';<% } %>
1717

1818
<%_ if(filters.babel) { -%>
1919
static parameters = [Http, SocketService];<% } %>
20-
constructor(<%= private() %>http: Http<% if(filters.socketio) { %>, <%= private() %>socketService: SocketService<% } %>) {
20+
constructor(<%= private() %>http: Http<% if(filters.ws) { %>, <%= private() %>socketService: SocketService<% } %>) {
2121
this.Http = http;
22-
<%_ if(filters.socketio) { -%>
22+
<%_ if(filters.ws) { -%>
2323
this.SocketService = socketService;<% } %>
2424
}
2525

2626
ngOnInit() {
2727
this.Http.get('/api/things')
2828
.map(res => {
2929
return res.json();
30-
<%_ if(filters.socketio) { -%>
31-
// this.SocketService.syncUpdates('thing', this.awesomeThings);<% } %>
3230
})
3331
.catch(err => Observable.throw(err.json().error || 'Server error'))
3432
.subscribe(things => {
3533
this.awesomeThings = things;
34+
<%_ if(filters.ws) { -%>
35+
this.SocketService.syncUpdates('thing', this.awesomeThings);<% } %>
3636
});
3737
}<% if (filters.models) { %>
38-
<%_ if(filters.socketio) { %>
38+
<%_ if(filters.ws) { %>
3939

4040
ngOnDestroy() {
4141
this.SocketService.unsyncUpdates('thing');
@@ -50,7 +50,7 @@ export class MainComponent implements OnInit<% if(filters.socketio) { %>, OnDest
5050
.map(res => res.json())
5151
.catch(err => Observable.throw(err.json().error || 'Server error'))
5252
.subscribe(thing => {
53-
this.awesomeThings.push(thing);
53+
console.log('Added Thing:', thing);
5454
});
5555
}
5656
}
@@ -60,7 +60,7 @@ export class MainComponent implements OnInit<% if(filters.socketio) { %>, OnDest
6060
.map(res => res.json())
6161
.catch(err => Observable.throw(err.json().error || 'Server error'))
6262
.subscribe(() => {
63-
this.awesomeThings.splice(this.awesomeThings.findIndex(el => el._id === thing._id), 1);
63+
console.log('Deleted Thing');
6464
});
6565
}<% } %>
6666
}

0 commit comments

Comments
 (0)