├── .browserslistrc
├── babel.config.js
├── public
├── favicon.ico
└── index.html
├── postcss.config.js
├── src
├── assets
│ ├── logo.png
│ └── css
│ │ └── loading-btn.css
├── App.vue
├── main.js
├── router.js
├── components
│ ├── UserList.vue
│ ├── RoomList.vue
│ ├── ChatNavBar.vue
│ ├── MessageList.vue
│ ├── MessageForm.vue
│ └── LoginForm.vue
├── store
│ ├── index.js
│ ├── mutations.js
│ └── actions.js
├── views
│ ├── Login.vue
│ └── Chat.vue
└── chatkit.js
├── .gitignore
├── package.json
└── README.md
/.browserslistrc:
--------------------------------------------------------------------------------
1 | > 1%
2 | last 2 versions
3 | not ie <= 8
4 |
--------------------------------------------------------------------------------
/babel.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | presets: [
3 | '@vue/app'
4 | ]
5 | }
6 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/brandiqa/vue-chatkit/HEAD/public/favicon.ico
--------------------------------------------------------------------------------
/postcss.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | plugins: {
3 | autoprefixer: {}
4 | }
5 | }
6 |
--------------------------------------------------------------------------------
/src/assets/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/brandiqa/vue-chatkit/HEAD/src/assets/logo.png
--------------------------------------------------------------------------------
/src/App.vue:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
10 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | node_modules
3 | /dist
4 |
5 | # local env files
6 | .env.local
7 | .env.*.local
8 |
9 | # Log files
10 | npm-debug.log*
11 | yarn-debug.log*
12 | yarn-error.log*
13 |
14 | # Editor directories and files
15 | .idea
16 | .vscode
17 | *.suo
18 | *.ntvs*
19 | *.njsproj
20 | *.sln
21 | *.sw*
22 |
--------------------------------------------------------------------------------
/src/main.js:
--------------------------------------------------------------------------------
1 | import Vue from 'vue'
2 | import BootstrapVue from 'bootstrap-vue'
3 | import VueChatScroll from 'vue-chat-scroll'
4 |
5 | import App from './App.vue'
6 | import router from './router'
7 | import store from './store/index'
8 |
9 | import 'bootstrap/dist/css/bootstrap.css'
10 | import 'bootstrap-vue/dist/bootstrap-vue.css'
11 | import './assets/css/loading.css'
12 | import './assets/css/loading-btn.css'
13 |
14 | Vue.config.productionTip = false
15 | Vue.use(BootstrapVue)
16 | Vue.use(VueChatScroll)
17 |
18 | new Vue({
19 | router,
20 | store,
21 | render: h => h(App)
22 | }).$mount('#app')
23 |
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | vue-chatkit
9 |
10 |
11 |
12 | We're sorry but vue-chatkit doesn't work properly without JavaScript enabled. Please enable it to continue.
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "vue-chatkit",
3 | "version": "0.1.0",
4 | "private": true,
5 | "scripts": {
6 | "serve": "vue-cli-service serve",
7 | "build": "vue-cli-service build"
8 | },
9 | "dependencies": {
10 | "@pusher/chatkit-client": "^1.3.2",
11 | "bootstrap": "^4.3.1",
12 | "bootstrap-vue": "^2.0.0-rc.13",
13 | "moment": "^2.24.0",
14 | "vue": "^2.6.8",
15 | "vue-chat-scroll": "^1.3.5",
16 | "vue-router": "^3.0.1",
17 | "vuex": "^3.1.0",
18 | "vuex-persist": "^2.0.0"
19 | },
20 | "devDependencies": {
21 | "@vue/cli-plugin-babel": "^3.4.1",
22 | "@vue/cli-service": "^3.4.1",
23 | "vue-template-compiler": "^2.6.8"
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/router.js:
--------------------------------------------------------------------------------
1 | import Vue from 'vue'
2 | import Router from 'vue-router'
3 | import Login from './views/Login.vue'
4 | import Chat from './views/Chat.vue'
5 | import store from './store'
6 |
7 | Vue.use(Router)
8 |
9 | export default new Router({
10 | mode: 'history',
11 | base: process.env.BASE_URL,
12 | routes: [
13 | {
14 | path: '/',
15 | name: 'login',
16 | component: Login
17 | },
18 | {
19 | path: '/chat',
20 | name: 'chat',
21 | component: Chat,
22 | beforeEnter: (to, from, next) => {
23 | if (store.state.user) {
24 | next();
25 | } else {
26 | next('/');
27 | }
28 | }
29 | }
30 | ]
31 | })
32 |
--------------------------------------------------------------------------------
/src/components/UserList.vue:
--------------------------------------------------------------------------------
1 |
2 |
3 |
Members
4 |
5 |
6 |
7 | {{ user.name }}
8 |
11 | {{ user.presence }}
12 |
13 |
14 |
15 |
16 |
17 |
35 |
--------------------------------------------------------------------------------
/src/store/index.js:
--------------------------------------------------------------------------------
1 | import Vue from 'vue'
2 | import Vuex from 'vuex'
3 | import VuexPersistence from 'vuex-persist'
4 | import mutations from './mutations'
5 | import actions from './actions'
6 |
7 | Vue.use(Vuex)
8 |
9 | const debug = process.env.NODE_ENV !== 'production'
10 |
11 | const vuexLocal = new VuexPersistence({
12 | storage: window.localStorage
13 | })
14 |
15 | export default new Vuex.Store({
16 | state: {
17 | loading: false,
18 | sending: false,
19 | error: null,
20 | user: null,
21 | reconnect: false,
22 | activeRoom: null,
23 | rooms: [],
24 | users: [],
25 | messages: [],
26 | userTyping: null
27 | },
28 | mutations,
29 | actions,
30 | getters: {
31 | hasError: state => state.error ? true : false
32 | },
33 | plugins: [vuexLocal.plugin],
34 | strict: debug
35 | })
36 |
--------------------------------------------------------------------------------
/src/views/Login.vue:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 | For more information visit website
8 | More Info
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
33 |
--------------------------------------------------------------------------------
/src/components/RoomList.vue:
--------------------------------------------------------------------------------
1 |
2 |
3 |
Channels
4 |
5 |
6 |
11 | # {{ room.name }}
12 |
13 |
14 |
15 |
16 |
17 |
38 |
--------------------------------------------------------------------------------
/src/store/mutations.js:
--------------------------------------------------------------------------------
1 | export default {
2 | setError(state, error) {
3 | state.error = error;
4 | },
5 | setLoading(state, loading) {
6 | state.loading = loading;
7 | },
8 | setUser(state, user) {
9 | state.user = user;
10 | },
11 | setReconnect(state, reconnect) {
12 | state.reconnect = reconnect;
13 | },
14 | setActiveRoom(state, roomId) {
15 | state.activeRoom = roomId;
16 | },
17 | setRooms(state, rooms) {
18 | state.rooms = rooms
19 | },
20 | setUsers(state, users) {
21 | state.users = users
22 | },
23 | clearChatRoom(state) {
24 | state.users = [];
25 | state.messages = [];
26 | },
27 | setMessages(state, messages) {
28 | state.messages = messages
29 | },
30 | addMessage(state, message) {
31 | state.messages.push(message)
32 | },
33 | setSending(state, status) {
34 | state.sending = status
35 | },
36 | setUserTyping(state, userId) {
37 | state.userTyping = userId
38 | },
39 | reset(state) {
40 | state.error = null;
41 | state.users = [];
42 | state.messages = [];
43 | state.rooms = [];
44 | state.user = null
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/components/ChatNavBar.vue:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Vue Chat
5 |
6 |
7 | {{ user.name }} |
8 | Logout
9 |
10 |
11 |
12 |
13 |
50 |
51 |
56 |
--------------------------------------------------------------------------------
/src/views/Chat.vue:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
56 |
--------------------------------------------------------------------------------
/src/components/MessageList.vue:
--------------------------------------------------------------------------------
1 |
2 |
3 |
Messages
4 |
5 |
6 |
7 |
8 |
{{ message.name }}
9 | @{{ message.username }}
10 |
11 |
12 | {{ message.text }}
13 |
14 |
15 | {{ message.date }}
16 |
17 |
18 |
19 |
@{{ userTyping }} is typing....
20 |
21 |
22 |
23 |
36 |
37 |
61 |
--------------------------------------------------------------------------------
/src/components/MessageForm.vue:
--------------------------------------------------------------------------------
1 |
2 |
24 |
25 |
26 |
64 |
--------------------------------------------------------------------------------
/src/store/actions.js:
--------------------------------------------------------------------------------
1 | import chatkit from '../chatkit';
2 |
3 | function handleError(commit, error) {
4 | const message = error.message || error.info.error_description;
5 | commit('setError', message);
6 | }
7 |
8 | export default {
9 | async login({ commit, state }, userId) {
10 | try {
11 | commit('setError', '');
12 | commit('setLoading', true);
13 | // Connect user to ChatKit service
14 | const currentUser = await chatkit.connectUser(userId);
15 | commit('setUser', {
16 | username: currentUser.id,
17 | name: currentUser.name
18 | });
19 | commit('setReconnect', false);
20 |
21 | // Save list of user's rooms in store
22 | const rooms = currentUser.rooms.map(room => ({
23 | id: room.id,
24 | name: room.name
25 | }))
26 | commit('setRooms', rooms);
27 |
28 | // Subscribe user to a room
29 | const activeRoom = state.activeRoom || rooms[0]; // pick last used room, or the first one
30 | commit('setActiveRoom', {
31 | id: activeRoom.id,
32 | name: activeRoom.name
33 | });
34 | await chatkit.subscribeToRoom(activeRoom.id);
35 |
36 | return true;
37 | } catch (error) {
38 | handleError(commit, error)
39 | } finally {
40 | commit('setLoading', false);
41 | }
42 | },
43 | async changeRoom({ commit }, roomId) {
44 | try {
45 | const { id, name } = await chatkit.subscribeToRoom(roomId);
46 | commit('setActiveRoom', { id, name });
47 | } catch (error) {
48 | handleError(commit, error)
49 | }
50 | },
51 | async sendMessage({ commit }, message) {
52 | try {
53 | commit('setError', '');
54 | commit('setSending', true);
55 | const messageId = await chatkit.sendMessage(message);
56 | return messageId;
57 | } catch (error) {
58 | handleError(commit, error)
59 | } finally {
60 | commit('setSending', false);
61 | }
62 | },
63 | async logout({ commit }) {
64 | commit('reset');
65 | chatkit.disconnectUser();
66 | window.localStorage.clear();
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Vue ChatKit
2 |
3 | This is a project tutorial on how to build a chat application using Vue.js and [Pusher Chatkit](https://pusher.com/chatkit). You can follow the tutorial by accessing this [book](https://www.sitepoint.com/premium/books/build-a-real-time-chat-app-with-pusher-and-vue-js).
4 |
5 | ## Project setup
6 |
7 | You'll need to create an account at Pusher Chatkit. Follow the instructions on the [book](https://www.sitepoint.com/premium/books/build-a-real-time-chat-app-with-pusher-and-vue-js) to learn how to set it all up
8 |
9 | ```bash
10 | npm install
11 | ```
12 |
13 | ### Compiles and hot-reloads for development
14 |
15 | ```bash
16 | npm run serve
17 | ```
18 |
19 | ### Compiles and minifies for production
20 |
21 | ```bash
22 | npm run build
23 | ```
24 |
25 | ### Run your tests
26 |
27 | ```bash
28 | npm run test
29 | ```
30 |
31 | ### Lint and fix files
32 |
33 | ```bash
34 | npm run lint
35 | ```
36 |
37 | ### Customize configuration
38 |
39 | See [Configuration Reference](https://cli.vuejs.org/config/).
40 |
41 | ## License
42 |
43 | The MIT License (MIT) Copyright (c)
44 |
45 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
46 |
47 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
48 |
49 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
50 |
--------------------------------------------------------------------------------
/src/components/LoginForm.vue:
--------------------------------------------------------------------------------
1 |
2 |
30 |
31 |
32 |
68 |
69 |
80 |
--------------------------------------------------------------------------------
/src/chatkit.js:
--------------------------------------------------------------------------------
1 | import { ChatManager, TokenProvider } from "@pusher/chatkit-client";
2 | import moment from "moment";
3 | import store from "./store/index";
4 |
5 | const INSTANCE_LOCATOR =
6 | process.env.VUE_APP_INSTANCE_LOCATOR ||
7 | "v1:us1:dd40dbb8-63fa-4081-901d-8d76fd697f4d";
8 | const TOKEN_URL =
9 | process.env.VUE_APP_TOKEN_URL ||
10 | "https://us1.pusherplatform.io/services/chatkit_token_provider/v1/dd40dbb8-63fa-4081-901d-8d76fd697f4d/token";
11 | const MESSAGE_LIMIT = parseInt(process.env.VUE_APP_MESSAGE_LIMIT) || 10;
12 |
13 | let currentUser = null;
14 | let activeRoom = null;
15 |
16 | async function connectUser(userId) {
17 | const chatManager = new ChatManager({
18 | instanceLocator: INSTANCE_LOCATOR,
19 | tokenProvider: new TokenProvider({ url: TOKEN_URL }),
20 | userId
21 | });
22 | currentUser = await chatManager.connect();
23 | return currentUser;
24 | }
25 |
26 | function setMembers() {
27 | const members = activeRoom.users.map(user => ({
28 | username: user.id,
29 | name: user.name,
30 | presence: user.presence.state
31 | }));
32 | store.commit("setUsers", members);
33 | }
34 |
35 | async function subscribeToRoom(roomId) {
36 | store.commit("clearChatRoom");
37 | activeRoom = await currentUser.subscribeToRoom({
38 | roomId,
39 | messageLimit: MESSAGE_LIMIT,
40 | hooks: {
41 | onMessage: message => {
42 | store.commit("addMessage", {
43 | name: message.sender.name,
44 | username: message.senderId,
45 | text: message.text,
46 | date: moment(message.createdAt).format("h:mm:ss a D-MM-YYYY")
47 | });
48 | },
49 | onPresenceChanged: () => {
50 | setMembers();
51 | },
52 | onUserStartedTyping: user => {
53 | store.commit("setUserTyping", user.id);
54 | },
55 | onUserStoppedTyping: () => {
56 | store.commit("setUserTyping", null);
57 | }
58 | }
59 | });
60 | setMembers();
61 | return activeRoom;
62 | }
63 |
64 | async function sendMessage(text) {
65 | const messageId = await currentUser.sendMessage({
66 | text,
67 | roomId: activeRoom.id
68 | });
69 | return messageId;
70 | }
71 |
72 | export function isTyping(roomId) {
73 | currentUser.isTypingIn({ roomId });
74 | }
75 |
76 | function disconnectUser() {
77 | currentUser.disconnect();
78 | }
79 |
80 | export default {
81 | connectUser,
82 | subscribeToRoom,
83 | sendMessage,
84 | isTyping,
85 | disconnectUser
86 | };
87 |
--------------------------------------------------------------------------------
/src/assets/css/loading-btn.css:
--------------------------------------------------------------------------------
1 | .ld-ext-right,
2 | .ld-ext-left,
3 | .ld-ext-bottom,
4 | .ld-ext-top,
5 | .ld-over,
6 | .ld-over-inverse,
7 | .ld-over-full,
8 | .ld-over-full-inverse {
9 | position: relative;
10 | -webkit-transition: all 0.3s;
11 | transition: all 0.3s;
12 | transition-timing-function: ease-in;
13 | overflow: hidden;
14 | }
15 | .ld-ext-right > .ld,
16 | .ld-ext-left > .ld,
17 | .ld-ext-bottom > .ld,
18 | .ld-ext-top > .ld,
19 | .ld-over > .ld,
20 | .ld-over-inverse > .ld,
21 | .ld-over-full > .ld,
22 | .ld-over-full-inverse > .ld {
23 | position: absolute;
24 | top: 50%;
25 | left: 50%;
26 | margin: -0.5em;
27 | opacity: 0;
28 | z-index: -100;
29 | -webkit-transition: all 0.3s;
30 | transition: all 0.3s;
31 | transition-timing-function: ease-in;
32 | }
33 | .ld-ext-right.running > .ld,
34 | .ld-ext-left.running > .ld,
35 | .ld-ext-bottom.running > .ld,
36 | .ld-ext-top.running > .ld,
37 | .ld-over.running > .ld,
38 | .ld-over-inverse.running > .ld,
39 | .ld-over-full.running > .ld,
40 | .ld-over-full-inverse.running > .ld {
41 | opacity: 1;
42 | z-index: auto;
43 | }
44 | .ld-ext-right.running {
45 | padding-right: 2.5em !important;
46 | }
47 | .ld-ext-right > .ld {
48 | top: 50%;
49 | left: auto;
50 | right: 1em;
51 | }
52 | .ld-ext-left.running {
53 | padding-left: 2.5em !important;
54 | }
55 | .ld-ext-left > .ld {
56 | top: 50%;
57 | right: auto;
58 | left: 1em;
59 | }
60 | .ld-ext-bottom.running {
61 | padding-bottom: 2.5em !important;
62 | }
63 | .ld-ext-bottom > .ld {
64 | top: auto;
65 | left: 50%;
66 | bottom: 1em;
67 | }
68 | .ld-ext-top.running {
69 | padding-top: 2.5em !important;
70 | }
71 | .ld-ext-top > .ld {
72 | bottom: auto;
73 | left: 50%;
74 | top: 1em;
75 | }
76 | .ld-over,
77 | .ld-over-inverse,
78 | .ld-over-full,
79 | .ld-over-full-inverse {
80 | overflow: hidden;
81 | }
82 | .ld-over.running > .ld,
83 | .ld-over-inverse.running > .ld,
84 | .ld-over-full.running > .ld,
85 | .ld-over-full-inverse.running > .ld {
86 | z-index: 99999;
87 | }
88 | .ld-over:before,
89 | .ld-over-inverse:before,
90 | .ld-over-full:before,
91 | .ld-over-full-inverse:before {
92 | content: " ";
93 | display: block;
94 | opacity: 0;
95 | position: absolute;
96 | z-index: -1;
97 | top: 0;
98 | left: 0;
99 | width: 100%;
100 | height: 100%;
101 | -webkit-transition: all 0.3s;
102 | transition: all 0.3s;
103 | transition-timing-function: ease-in;
104 | background: rgba(240,240,240,0.8);
105 | }
106 | .ld-over-full > .ld,
107 | .ld-over-full-inverse > .ld {
108 | position: fixed;
109 | }
110 | .ld-over-full > .ld {
111 | color: rgba(0,0,0,0.8);
112 | }
113 | .ld-over-full:before,
114 | .ld-over-full-inverse:before {
115 | z-index: -1;
116 | position: fixed;
117 | background: rgba(255,255,255,0.8);
118 | }
119 | .ld-over.running > .ld,
120 | .ld-over-inverse.running > .ld,
121 | .ld-over-full.running > .ld,
122 | .ld-over-full-inverse.running > .ld {
123 | z-index: 999999;
124 | }
125 | .ld-over.running:before,
126 | .ld-over-inverse.running:before,
127 | .ld-over-full.running:before,
128 | .ld-over-full-inverse.running:before {
129 | opacity: 1;
130 | z-index: 999998;
131 | display: block;
132 | }
133 | .ld-over-inverse > .ld {
134 | color: rgba(255,255,255,0.8);
135 | }
136 | .ld-over-inverse:before {
137 | background: rgba(0,0,0,0.6);
138 | }
139 | .ld-over-full-inverse > .ld {
140 | color: rgba(255,255,255,0.8);
141 | }
142 | .ld-over-full-inverse:before {
143 | background: rgba(0,0,0,0.6);
144 | }
145 |
--------------------------------------------------------------------------------