Skip to content

Commit 376cfc0

Browse files
committed
No need for 'register' event
Removed the 'register' event as the user details are now being passed as part of the initial socket setup.
1 parent 53d75c1 commit 376cfc0

3 files changed

Lines changed: 19 additions & 31 deletions

File tree

app/socket/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ module.exports = (server, redis) => {
2424
allowEIO3: true,
2525
cors: {
2626
origin: '*',
27-
methods: ['GET', 'POST']
28-
}
27+
methods: ['GET', 'POST'],
28+
credentials: true
29+
},
2930
});
3031
const handlers = Handlers(activityService, socketServer);
3132
const watcher = redis.duplicate();

app/socket/router/index.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,7 @@ const router = {
2525
return [...connections];
2626
},
2727
init: (io, iorouter, handlers) => {
28-
const start = Date.now();
2928
// Set up routes for each type of message.
30-
iorouter.on('register', (socket, ctx, next) => {
31-
utils.log(socket, ctx.request.user, 'register');
32-
router.addUser(socket.id, ctx.request.user);
33-
next();
34-
});
3529
iorouter.on('view', (socket, ctx, next) => {
3630
const user = router.getUser(socket.id);
3731
utils.log(socket, `${ctx.request.caseId} (${user.name})`, 'view');
@@ -54,15 +48,16 @@ const router = {
5448
// On client connection, attach the router and track the socket.
5549
io.on('connection', (socket) => {
5650
router.addConnection(socket);
57-
const ts = (Date.now() - start).toString(10).padStart(10, '0');
58-
utils.log(socket, '', `connected (${router.getConnections().length} total)`, console.log, ts);
51+
router.addUser(socket.id, socket.handshake.query.user);
52+
utils.log(socket, '', `connected (${router.getConnections().length} total)`);
53+
utils.log(socket, '', `connected (${router.getConnections().length} total)`, console.log, Date.now());
5954
socket.use((packet, next) => {
6055
iorouter.attach(socket, packet, next);
6156
});
6257
// When the socket disconnects, do an appropriate teardown.
6358
socket.on('disconnect', () => {
64-
const ts = (Date.now() - start).toString(10).padStart(10, '0');
65-
utils.log(socket, '', `disconnected (${router.getConnections().length - 1} total)`, console.log, ts);
59+
utils.log(socket, '', `disconnected (${router.getConnections().length - 1} total)`);
60+
utils.log(socket, '', `disconnected (${router.getConnections().length - 1} total)`, console.log, Date.now());
6661
handlers.removeSocketActivity(socket.id);
6762
router.removeUser(socket.id);
6863
router.removeConnection(socket);

test/spec/app/socket/router/index.spec.js

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ describe('socket.router', () => {
4747
};
4848
const MOCK_SOCKET = {
4949
id: 'socket-id',
50+
handshake: {
51+
query: {
52+
user: { id: 'a', name: 'Bob Smith' }
53+
}
54+
},
5055
rooms: ['socket-id'],
5156
events: {},
5257
messages: [],
@@ -101,31 +106,26 @@ describe('socket.router', () => {
101106
});
102107
});
103108
it('should have set up the appropriate events on the io router', () => {
104-
const EXPECTED_EVENTS = ['register', 'view', 'edit', 'watch'];
109+
const EXPECTED_EVENTS = ['view', 'edit', 'watch'];
105110
EXPECTED_EVENTS.forEach((event) => {
106111
expect(MOCK_IO_ROUTER.events[event]).to.be.a('function');
107112
});
108113
});
109114
});
110115

111116
describe('iorouter', () => {
112-
const MOCK_CONTEXT_REGISTER = {
113-
request: {
114-
user: { id: 'a', name: 'Bob Smith' }
115-
}
116-
};
117117
const MOCK_CONTEXT = {
118118
request: {
119119
caseId: '1234567890',
120120
caseIds: ['2345678901', '3456789012', '4567890123']
121121
}
122122
};
123123
beforeEach(() => {
124-
// We need to register before each call as it sets up the user.
125-
MOCK_IO_ROUTER.dispatch('register', MOCK_SOCKET, MOCK_CONTEXT_REGISTER, () => {});
124+
// Dispatch the connection each time.
125+
MOCK_SOCKET_SERVER.dispatch('connection', MOCK_SOCKET);
126126
});
127127
it('should appropriately handle registering a user', () => {
128-
expect(router.getUser(MOCK_SOCKET.id)).to.deep.equal(MOCK_CONTEXT_REGISTER.request.user);
128+
expect(router.getUser(MOCK_SOCKET.id)).to.deep.equal(MOCK_SOCKET.handshake.query.user);
129129
});
130130
it('should appropriately handle viewing a case', () => {
131131
const ACTIVITY = 'view';
@@ -138,7 +138,7 @@ describe('socket.router', () => {
138138
expect(MOCK_HANDLERS.calls[0].params.socket).to.equal(MOCK_SOCKET);
139139
expect(MOCK_HANDLERS.calls[0].params.caseId).to.equal(MOCK_CONTEXT.request.caseId);
140140
// Note that the MOCK_CONTEXT doesn't include the user, which means we had to get it from elsewhere.
141-
expect(MOCK_HANDLERS.calls[0].params.user).to.deep.equal(MOCK_CONTEXT_REGISTER.request.user);
141+
expect(MOCK_HANDLERS.calls[0].params.user).to.deep.equal(MOCK_SOCKET.handshake.query.user);
142142
expect(MOCK_HANDLERS.calls[0].params.activity).to.equal(ACTIVITY);
143143
});
144144
expect(nextCalled).to.be.true;
@@ -154,7 +154,7 @@ describe('socket.router', () => {
154154
expect(MOCK_HANDLERS.calls[0].params.socket).to.equal(MOCK_SOCKET);
155155
expect(MOCK_HANDLERS.calls[0].params.caseId).to.equal(MOCK_CONTEXT.request.caseId);
156156
// Note that the MOCK_CONTEXT doesn't include the user, which means we had to get it from elsewhere.
157-
expect(MOCK_HANDLERS.calls[0].params.user).to.deep.equal(MOCK_CONTEXT_REGISTER.request.user);
157+
expect(MOCK_HANDLERS.calls[0].params.user).to.deep.equal(MOCK_SOCKET.handshake.query.user);
158158
expect(MOCK_HANDLERS.calls[0].params.activity).to.equal(ACTIVITY);
159159
});
160160
expect(nextCalled).to.be.true;
@@ -175,15 +175,7 @@ describe('socket.router', () => {
175175
});
176176

177177
describe('io', () => {
178-
const MOCK_CONTEXT_REGISTER = {
179-
request: {
180-
user: { id: 'a', name: 'Bob Smith' }
181-
}
182-
};
183178
beforeEach(() => {
184-
// We need to register before each call as it sets up the user.
185-
MOCK_IO_ROUTER.dispatch('register', MOCK_SOCKET, MOCK_CONTEXT_REGISTER, () => {});
186-
187179
// Dispatch the connection each time.
188180
MOCK_SOCKET_SERVER.dispatch('connection', MOCK_SOCKET);
189181
});

0 commit comments

Comments
 (0)