akarshanxyz 1.0.1 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,266 @@
1
+ const express = require("express");
2
+ const http = require("http");
3
+ const { Server } = require("socket.io");
4
+
5
+ const app = express();
6
+ const server = http.createServer(app);
7
+ const io = new Server(server);
8
+
9
+ let connectedUsers = 0;
10
+
11
+ app.get("/", (req, res) => {
12
+ res.send(`
13
+ <!DOCTYPE html>
14
+ <html>
15
+ <head>
16
+ <title>Real-Time Chat App</title>
17
+ <script src="/socket.io/socket.io.js"></script>
18
+ <style>
19
+ body{
20
+ font-family:Arial;
21
+ margin:20px;
22
+ }
23
+ nav button{
24
+ margin-right:10px;
25
+ padding:8px 15px;
26
+ }
27
+ .page{
28
+ display:none;
29
+ margin-top:20px;
30
+ }
31
+ .active{
32
+ display:block;
33
+ }
34
+ input{
35
+ margin:5px;
36
+ padding:5px;
37
+ }
38
+ #messages,#roomMessages{
39
+ border:1px solid black;
40
+ height:200px;
41
+ overflow-y:auto;
42
+ padding:10px;
43
+ margin-top:10px;
44
+ }
45
+ </style>
46
+ </head>
47
+ <body>
48
+
49
+ <h1>Real-Time Chat Application</h1>
50
+
51
+ <nav>
52
+ <button onclick="showPage('home')">Home</button>
53
+ <button onclick="showPage('private')">Private Chat</button>
54
+ <button onclick="showPage('room')">Room Chat</button>
55
+ </nav>
56
+
57
+ <!-- HOME -->
58
+ <div id="home" class="page active">
59
+ <h2>Home</h2>
60
+
61
+ <p><b>Your Socket ID:</b></p>
62
+ <p id="socketId"></p>
63
+
64
+ <p><b>Connected Users:</b></p>
65
+ <p id="userCount">0</p>
66
+
67
+ <h3>Notifications</h3>
68
+ <div id="notifications"></div>
69
+ </div>
70
+
71
+ <!-- PRIVATE CHAT -->
72
+ <div id="private" class="page">
73
+ <h2>Private Chat</h2>
74
+
75
+ <input id="receiverId" placeholder="Receiver Socket ID">
76
+
77
+ <input id="privateMessage" placeholder="Message">
78
+
79
+ <button onclick="sendPrivateMessage()">
80
+ Send
81
+ </button>
82
+
83
+ <div id="messages"></div>
84
+ </div>
85
+
86
+ <!-- ROOM CHAT -->
87
+ <div id="room" class="page">
88
+ <h2>Room Chat</h2>
89
+
90
+ <input id="roomName" placeholder="Room Name">
91
+
92
+ <button onclick="joinRoom()">
93
+ Join Room
94
+ </button>
95
+
96
+ <br>
97
+
98
+ <input id="roomMessage" placeholder="Room Message">
99
+
100
+ <button onclick="sendRoomMessage()">
101
+ Send
102
+ </button>
103
+
104
+ <div id="roomMessages"></div>
105
+ </div>
106
+
107
+ <script>
108
+
109
+ const socket = io();
110
+
111
+ let currentRoom = "";
112
+
113
+ function showPage(page){
114
+ document.querySelectorAll(".page")
115
+ .forEach(p => p.classList.remove("active"));
116
+
117
+ document
118
+ .getElementById(page)
119
+ .classList.add("active");
120
+ }
121
+
122
+ socket.on("connect",()=>{
123
+ document.getElementById("socketId").innerText = socket.id;
124
+ });
125
+
126
+ socket.on("userCount",(count)=>{
127
+ document.getElementById("userCount").innerText = count;
128
+ });
129
+
130
+ socket.on("welcome",(msg)=>{
131
+ const div = document.createElement("div");
132
+ div.innerText = msg;
133
+ document
134
+ .getElementById("notifications")
135
+ .appendChild(div);
136
+ });
137
+
138
+ function sendPrivateMessage(){
139
+
140
+ const receiver =
141
+ document.getElementById("receiverId").value;
142
+
143
+ const message =
144
+ document.getElementById("privateMessage").value;
145
+
146
+ socket.emit("privateMessage",{
147
+ receiver,
148
+ message
149
+ });
150
+ }
151
+
152
+ socket.on("privateMessage",(data)=>{
153
+
154
+ const div = document.createElement("div");
155
+
156
+ div.innerText =
157
+ data.sender + ": " + data.message;
158
+
159
+ document
160
+ .getElementById("messages")
161
+ .appendChild(div);
162
+ });
163
+
164
+ function joinRoom(){
165
+
166
+ currentRoom =
167
+ document.getElementById("roomName").value;
168
+
169
+ socket.emit("joinRoom", currentRoom);
170
+ }
171
+
172
+ function sendRoomMessage(){
173
+
174
+ const msg =
175
+ document.getElementById("roomMessage").value;
176
+
177
+ socket.emit("roomMessage",{
178
+ room: currentRoom,
179
+ message: msg
180
+ });
181
+ }
182
+
183
+ socket.on("roomMessage",(data)=>{
184
+
185
+ const div = document.createElement("div");
186
+
187
+ div.innerText =
188
+ data.sender + ": " + data.message;
189
+
190
+ document
191
+ .getElementById("roomMessages")
192
+ .appendChild(div);
193
+ });
194
+
195
+ socket.on("roomNotification",(msg)=>{
196
+
197
+ const div = document.createElement("div");
198
+
199
+ div.innerText = msg;
200
+
201
+ document
202
+ .getElementById("roomMessages")
203
+ .appendChild(div);
204
+ });
205
+
206
+ </script>
207
+
208
+ </body>
209
+ </html>
210
+ `);
211
+ });
212
+
213
+ io.on("connection", (socket) => {
214
+
215
+ connectedUsers++;
216
+
217
+ io.emit("userCount", connectedUsers);
218
+
219
+ io.emit(
220
+ "welcome",
221
+ "User joined: " + socket.id
222
+ );
223
+
224
+ socket.on("privateMessage", (data) => {
225
+
226
+ io.to(data.receiver).emit(
227
+ "privateMessage",
228
+ {
229
+ sender: socket.id,
230
+ message: data.message
231
+ }
232
+ );
233
+ });
234
+
235
+ socket.on("joinRoom", (room) => {
236
+
237
+ socket.join(room);
238
+
239
+ socket.to(room).emit(
240
+ "roomNotification",
241
+ socket.id + " joined room " + room
242
+ );
243
+ });
244
+
245
+ socket.on("roomMessage", (data) => {
246
+
247
+ io.to(data.room).emit(
248
+ "roomMessage",
249
+ {
250
+ sender: socket.id,
251
+ message: data.message
252
+ }
253
+ );
254
+ });
255
+
256
+ socket.on("disconnect", () => {
257
+
258
+ connectedUsers--;
259
+
260
+ io.emit("userCount", connectedUsers);
261
+ });
262
+ });
263
+
264
+ server.listen(3000, () => {
265
+ console.log("Server running on port 3000");
266
+ });
@@ -0,0 +1,85 @@
1
+ import express from "express";
2
+ import { graphqlHTTP } from "express-graphql";
3
+ import {
4
+ GraphQLObjectType,
5
+ GraphQLSchema,
6
+ GraphQLString,
7
+ GraphQLInt
8
+ } from "graphql";
9
+
10
+ /* ---------------- Dummy Data ---------------- */
11
+
12
+ const user = {
13
+ name: "Amit Sharma",
14
+ age: 21,
15
+ course: {
16
+ courseName: "Full Stack Development",
17
+ duration: "6 Months"
18
+ }
19
+ };
20
+
21
+ /* ---------------- Course Type ---------------- */
22
+
23
+ const CourseType = new GraphQLObjectType({
24
+ name: "Course",
25
+ fields: () => ({
26
+ courseName: { type: GraphQLString },
27
+ duration: { type: GraphQLString }
28
+ })
29
+ });
30
+
31
+ /* ---------------- User Type ---------------- */
32
+
33
+ const UserType = new GraphQLObjectType({
34
+ name: "User",
35
+ fields: () => ({
36
+ name: { type: GraphQLString },
37
+ age: { type: GraphQLInt },
38
+
39
+ // Nested Object
40
+ course: {
41
+ type: CourseType,
42
+ resolve(parent) {
43
+ return parent.course;
44
+ }
45
+ }
46
+ })
47
+ });
48
+
49
+ /* ---------------- Root Query ---------------- */
50
+
51
+ const RootQuery = new GraphQLObjectType({
52
+ name: "Query",
53
+ fields: {
54
+ user: {
55
+ type: UserType,
56
+ resolve() {
57
+ return user;
58
+ }
59
+ }
60
+ }
61
+ });
62
+
63
+ /* ---------------- Schema ---------------- */
64
+
65
+ const schema = new GraphQLSchema({
66
+ query: RootQuery
67
+ });
68
+
69
+ /* ---------------- Server ---------------- */
70
+
71
+ const app = express();
72
+
73
+ app.use(
74
+ "/graphql",
75
+ graphqlHTTP({
76
+ schema,
77
+ graphiql: true
78
+ })
79
+ );
80
+
81
+ app.listen(4000, () => {
82
+ console.log(
83
+ "Server running at http://localhost:4000/graphql"
84
+ );
85
+ });
@@ -0,0 +1,313 @@
1
+ import graphql from "graphql";
2
+
3
+ const {
4
+ GraphQLObjectType,
5
+ GraphQLSchema,
6
+ GraphQLInt,
7
+ GraphQLString,
8
+ GraphQLList
9
+ } = graphql;
10
+
11
+ /* ------------------ Dummy Data ------------------ */
12
+
13
+ const users = [
14
+ {
15
+ id: 1,
16
+ name: "Amit Sharma",
17
+ email: "amit@gmail.com",
18
+ city: "Delhi"
19
+ },
20
+ {
21
+ id: 2,
22
+ name: "Priya Verma",
23
+ email: "priya@gmail.com",
24
+ city: "Mumbai"
25
+ },
26
+ {
27
+ id: 3,
28
+ name: "Rahul Mehta",
29
+ email: "rahul@gmail.com",
30
+ city: "Bangalore"
31
+ }
32
+ ];
33
+
34
+ const queries = [
35
+ {
36
+ id: 1,
37
+ userId: 1,
38
+ title: "How to reset password?"
39
+ },
40
+ {
41
+ id: 2,
42
+ userId: 1,
43
+ title: "Unable to update profile"
44
+ },
45
+ {
46
+ id: 3,
47
+ userId: 2,
48
+ title: "Login authentication issue"
49
+ }
50
+ ];
51
+
52
+ /* ------------------ Types ------------------ */
53
+
54
+ const QueryType = new GraphQLObjectType({
55
+ name: "SupportQuery",
56
+ fields: () => ({
57
+ id: { type: GraphQLInt },
58
+ userId: { type: GraphQLInt },
59
+ title: { type: GraphQLString }
60
+ })
61
+ });
62
+
63
+ const UserType = new GraphQLObjectType({
64
+ name: "User",
65
+ fields: () => ({
66
+ id: { type: GraphQLInt },
67
+ name: { type: GraphQLString },
68
+ email: { type: GraphQLString },
69
+ city: { type: GraphQLString },
70
+
71
+ // Task 6
72
+ queries: {
73
+ type: new GraphQLList(QueryType),
74
+ resolve(parent) {
75
+ return queries.filter(q => q.userId === parent.id);
76
+ }
77
+ }
78
+ })
79
+ });
80
+
81
+ const UserQueryCountType = new GraphQLObjectType({
82
+ name: "UserQueryCount",
83
+ fields: () => ({
84
+ name: { type: GraphQLString },
85
+ totalQueries: { type: GraphQLInt }
86
+ })
87
+ });
88
+
89
+ /* ------------------ Root Query ------------------ */
90
+
91
+ const RootQuery = new GraphQLObjectType({
92
+ name: "Query",
93
+ fields: {
94
+ // Task 2
95
+ users: {
96
+ type: new GraphQLList(UserType),
97
+ resolve() {
98
+ return users;
99
+ }
100
+ },
101
+
102
+ // Task 3
103
+ user: {
104
+ type: UserType,
105
+ args: {
106
+ id: { type: GraphQLInt }
107
+ },
108
+ resolve(_, args) {
109
+ return users.find(user => user.id === args.id);
110
+ }
111
+ },
112
+
113
+ // Task 4
114
+ supportQueries: {
115
+ type: new GraphQLList(QueryType),
116
+ resolve() {
117
+ return queries;
118
+ }
119
+ },
120
+
121
+ // Task 5
122
+ usersByCity: {
123
+ type: new GraphQLList(UserType),
124
+ args: {
125
+ city: { type: GraphQLString }
126
+ },
127
+ resolve(_, args) {
128
+ return users.filter(
129
+ user =>
130
+ user.city.toLowerCase() === args.city.toLowerCase()
131
+ );
132
+ }
133
+ },
134
+
135
+ // Task 13
136
+ totalUsers: {
137
+ type: GraphQLInt,
138
+ resolve() {
139
+ return users.length;
140
+ }
141
+ },
142
+
143
+ // Task 14
144
+ totalSupportQueries: {
145
+ type: GraphQLInt,
146
+ resolve() {
147
+ return queries.length;
148
+ }
149
+ },
150
+
151
+ // Task 15
152
+ searchUser: {
153
+ type: new GraphQLList(UserType),
154
+ args: {
155
+ name: { type: GraphQLString }
156
+ },
157
+ resolve(_, args) {
158
+ return users.filter(user =>
159
+ user.name
160
+ .toLowerCase()
161
+ .includes(args.name.toLowerCase())
162
+ );
163
+ }
164
+ },
165
+
166
+ // Task 16
167
+ usersWithQueryCount: {
168
+ type: new GraphQLList(UserQueryCountType),
169
+ resolve() {
170
+ return users.map(user => ({
171
+ name: user.name,
172
+ totalQueries: queries.filter(
173
+ q => q.userId === user.id
174
+ ).length
175
+ }));
176
+ }
177
+ }
178
+ }
179
+ });
180
+
181
+ /* ------------------ Mutations ------------------ */
182
+
183
+ const Mutation = new GraphQLObjectType({
184
+ name: "Mutation",
185
+ fields: {
186
+ // Task 7
187
+ addUser: {
188
+ type: UserType,
189
+ args: {
190
+ name: { type: GraphQLString },
191
+ email: { type: GraphQLString },
192
+ city: { type: GraphQLString }
193
+ },
194
+ resolve(_, args) {
195
+ const user = {
196
+ id: users.length + 1,
197
+ name: args.name,
198
+ email: args.email,
199
+ city: args.city
200
+ };
201
+
202
+ users.push(user);
203
+ return user;
204
+ }
205
+ },
206
+
207
+ // Task 8
208
+ addSupportQuery: {
209
+ type: QueryType,
210
+ args: {
211
+ userId: { type: GraphQLInt },
212
+ title: { type: GraphQLString }
213
+ },
214
+ resolve(_, args) {
215
+ const query = {
216
+ id: queries.length + 1,
217
+ userId: args.userId,
218
+ title: args.title
219
+ };
220
+
221
+ queries.push(query);
222
+ return query;
223
+ }
224
+ },
225
+
226
+ // Task 9
227
+ updateUserCity: {
228
+ type: UserType,
229
+ args: {
230
+ id: { type: GraphQLInt },
231
+ city: { type: GraphQLString }
232
+ },
233
+ resolve(_, args) {
234
+ const user = users.find(
235
+ user => user.id === args.id
236
+ );
237
+
238
+ if (user) {
239
+ user.city = args.city;
240
+ }
241
+
242
+ return user;
243
+ }
244
+ },
245
+
246
+ // Task 10
247
+ updateUserEmail: {
248
+ type: UserType,
249
+ args: {
250
+ id: { type: GraphQLInt },
251
+ email: { type: GraphQLString }
252
+ },
253
+ resolve(_, args) {
254
+ const user = users.find(
255
+ user => user.id === args.id
256
+ );
257
+
258
+ if (user) {
259
+ user.email = args.email;
260
+ }
261
+
262
+ return user;
263
+ }
264
+ },
265
+
266
+ // Task 11
267
+ deleteUser: {
268
+ type: UserType,
269
+ args: {
270
+ id: { type: GraphQLInt }
271
+ },
272
+ resolve(_, args) {
273
+ const index = users.findIndex(
274
+ user => user.id === args.id
275
+ );
276
+
277
+ if (index === -1) return null;
278
+
279
+ const deletedUser = users[index];
280
+ users.splice(index, 1);
281
+
282
+ return deletedUser;
283
+ }
284
+ },
285
+
286
+ // Task 12
287
+ deleteSupportQuery: {
288
+ type: QueryType,
289
+ args: {
290
+ id: { type: GraphQLInt }
291
+ },
292
+ resolve(_, args) {
293
+ const index = queries.findIndex(
294
+ query => query.id === args.id
295
+ );
296
+
297
+ if (index === -1) return null;
298
+
299
+ const deletedQuery = queries[index];
300
+ queries.splice(index, 1);
301
+
302
+ return deletedQuery;
303
+ }
304
+ }
305
+ }
306
+ });
307
+
308
+ /* ------------------ Schema ------------------ */
309
+
310
+ export default new GraphQLSchema({
311
+ query: RootQuery,
312
+ mutation: Mutation
313
+ });