Skip to content

Commit

Permalink
Add socket namespace (adrigardi90#4)
Browse files Browse the repository at this point in the history
* Socket namespace
  • Loading branch information
adrigardi90 authored May 24, 2019
1 parent dbfb9f6 commit cb79ee7
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 17 deletions.
13 changes: 6 additions & 7 deletions server/chat_namespace/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const joinRoom = (socket, namespace) => ({ username, room, status }) => {
if (users === null) return

// Notify all the users in the same room
namespace.sockets.in(room).emit('newUser', { users, username });
namespace.in(room).emit('newUser', { users, username });
})
});

Expand All @@ -34,12 +34,12 @@ const changeStatus = (socket, namespace) => ({ username, status, room }) => {
.then(users => {
if (users === null) return
// Notify all the users in the same room
namespace.sockets.in(room).emit('newUser', { users, username });
namespace.in(room).emit('newUser', { users, username });
})
}

const publicMessage = (namespace) => ({ room, message, username }) => {
namespace.sockets.in(room).emit('newMessage', {
namespace.in(room).emit('newMessage', {
message,
username
});
Expand All @@ -51,7 +51,6 @@ const leaveRoom = (socket, namespace) => ({ room, username }) => {
socket.leave(room, () => {
console.log(`user ${username} left the room ${room}`);


ChatRedis.delUser(room, socket.id)
.then(data => {
if (data === null) return null
Expand All @@ -61,7 +60,7 @@ const leaveRoom = (socket, namespace) => ({ room, username }) => {
if (users === null) return

// Notify all the users in the same room
namespace.sockets.in(room).emit('newUser', { users, username });
namespace.in(room).emit('newUser', { users, username });
})

})
Expand All @@ -79,7 +78,7 @@ const leaveChat = (socket, namespace) => ({ room, username }) => {
if (users === null) return

// Notify all the users in the same room
namespace.sockets.in(room).emit('leaveChat', { users, username });
namespace.in(room).emit('leaveChat', { users, username });

// Leave the socket
socket.leave(room, () => {
Expand Down Expand Up @@ -128,7 +127,7 @@ const joinPrivateRoom = (socket, namespace) => ({ username, room, to }) => {
if (res === null) return

// Notify the user to talk with (in the same main room)
namespace.sockets.in(room).emit('privateChat', {
namespace.in(room).emit('privateChat', {
username,
to
});
Expand Down
5 changes: 2 additions & 3 deletions server/chat_namespace/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let namespace;
const onConnection = (socket) => {

console.log(`Socket connected to port ${config.PORT}`)

// Listening for joining a room
socket.on('joinRoom', events.joinRoom(socket, namespace));

Expand Down Expand Up @@ -40,7 +40,6 @@ const onConnection = (socket) => {

exports.createNameSpace = (io) => {
namespace = io
namespace
// .of(config.CHAT_NAMESPACE)
.of(config.CHAT_NAMESPACE)
.on('connection', onConnection)
}
2 changes: 1 addition & 1 deletion server/config/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

const CONFIG = {
PORT: process.env.PORT || 3000,
CHAT_NAMESPACE: '/chat',
CHAT_NAMESPACE: '/video-chat',
REDIS_HOST: process.env.REDIS_HOST || 'localhost',
REDIS_PORT: process.env.REDIS_PORT || 6379,
ORIGINS: process.env.ORIGINS || '*:*'
Expand Down
3 changes: 0 additions & 3 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ app.io.origins([config.ORIGINS])
// Using the adapter to pass event between nodes
app.io.adapter(redis({ host: config.REDIS_HOST, port: config.REDIS_PORT }));

// Using the adapter to pass event between nodes
app.io.adapter(redis({ host: config.REDIS_HOST, port: config.REDIS_PORT }));

server.listen(config.PORT, () => {
console.log(`Server Listening on port ${config.PORT}`)
});
2 changes: 1 addition & 1 deletion server/redis/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function ChatRedis() {
*/
ChatRedis.prototype.addUser = function (room, socketId, userObject) {
this.client.hsetAsync(room, socketId, JSON.stringify(userObject)).then(
() => console.debug('addUser', userObject.username + 'added to the room ' + room),
() => console.debug('addUser ', userObject.username + ' added to the room ' + room),
err => console.log('addUser', err)
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { url } from './utils/config'
// Socket config
Vue.use(new VueSocketIO({
debug: true,
connection: url,
connection: `${url}/video-chat`,
vuex: {
store,
actionPrefix: 'SOCKET_',
mutationPrefix: 'SOCKET_'
}
},
}))
// Vue resource for http
Vue.use(VueResource)
Expand Down

0 comments on commit cb79ee7

Please sign in to comment.