You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Users will be able to chat about items. If you have experience with Offerup or similar apps, that will be extremely valuable for this. When two users start talking about an item it will create a chat room. Every chat room will "belong" to an item and a pair of users. Every message will "belong" to that chat room. The models will look something like this, please familiarize yourselves with it. We will come back to the database later, not this week.
Before we save the messages, lets start sending some. We will use socket.io for instant messaging.
To install socket.io on the backend run: npm install socket.io
Here is some starter code for the backend/server.js
// web socket for chat
const http = require("http");
const server = http.createServer(app);
app.use(cors());
const { Server } = require("socket.io");
const io = new Server(server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
},
});
io.on("connection", (socket) => {
console.log("User connected:", socket.id);
socket.on("sendMessage", (message) => {
io.emit("receiveMessage", message); // Broadcast to all clients
});
socket.on("disconnect", () => {
console.log("User disconnected:", socket.id);
});
});
const PORT = process.env.PORT || 0; // 0 lets the OS assign an available port
server.listen(PORT, () => {
console.log(`Server running on port ${server.address().port}`);
});
Front End
Now install socket.io-client for the front end. Run: npm install socket.io-client
Create a new page called ChatPage.tsx on the front end in the src/pages folder. Soon we should also make a Messages.tsx page to view all the different chat rooms going on. Here is some starter code for the new page:
Test and iterate. It looks like the server is going to use some available port and will have to notify the front end which port to use. Use plenty of console.log() to see what is going on.
The text was updated successfully, but these errors were encountered:
Users will be able to chat about items. If you have experience with Offerup or similar apps, that will be extremely valuable for this. When two users start talking about an item it will create a chat room. Every chat room will "belong" to an item and a pair of users. Every message will "belong" to that chat room. The models will look something like this, please familiarize yourselves with it. We will come back to the database later, not this week.
Before we save the messages, lets start sending some. We will use socket.io for instant messaging.
To install socket.io on the backend run:
npm install socket.io
Here is some starter code for the backend/server.js
Front End
Now install socket.io-client for the front end. Run:
npm install socket.io-client
Create a new page called ChatPage.tsx on the front end in the src/pages folder. Soon we should also make a Messages.tsx page to view all the different chat rooms going on. Here is some starter code for the new page:
The text was updated successfully, but these errors were encountered: