Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

node.js - Socket.io integration with Express.js

I trying to create a app where end user will request for services after that notification will be sent to the service provider, once the service provider accepts the user request user will be notified via notification that his request is accepted by one of the service provider.

Below is the stack which I am using. Node.js, Express.js, Socket.io, React.js

below is the code for server.js

const express  = require('express')
const app = express()
const server = require('http').createServer(app);
const io = require('socket.io')(server)
const cors = require('cors')
var routes = require('./routes/routes')
const port = process.env.PORT || 8080
app.use(express.json({ limit: "100mb" }))
app.use(express.urlencoded({ extended: false }))
app.use(cors())
app.use(routes)
app.use(express.static('uploads'))
app.io = io

server.listen(port,()=>{
    console.log('API server started on: ',port)
})

There is a booking api which is accepting the booking from user.

I want to know how I have to write code for sending notification to service provider.

This is how I am trying to generate notification when api request comes.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can sort out this problem by creating a unique room in which only the service provider is conneced.

Once the service provider joins the socket You can emit an event from that service provider mentioning the unique room. (name of the room can be the unique id of the service provider) Then the service provider itself joins that specific room. when the client books from that service provider then the client emits an event mentioning that specific room which he gets from the service provider data. Once the client completes the booking in the server then the client emits an event to that specific room. If the service provider is already connected to that room then he/she will get the notification from that room (which is the unique id of the service provider).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...