seitbekir / sockrest Goto Github PK
View Code? Open in Web Editor NEWSocket server that works inhibiting expressJS philosophy (WIP)
License: GNU General Public License v3.0
Socket server that works inhibiting expressJS philosophy (WIP)
License: GNU General Public License v3.0
Server need a connections and users container, to make it possible for horizontal scalins, making subscriptions plugins and ability to send messages to other connections.
Part that will store and share connections and users by id is Adapter.
Needs to add functions to push messages to clients.
As a developer interested to working with users, that might be connected from many devices (also, on different server instances if server is scaled), service has to give approach, that will encapsulate this.
Connection and user instance has to be available from req
var.
example of user instance:
app.use((req, res, next) => {
req.user.push({
message: 'single message from server',
})
})
example of connection instance:
app.use((req, res, next) => {
req.connection.push({
message: 'single message from server for only current connection',
})
})
availability of user from API:
app.use((req, res, next) => {
let someUserId = 25;
req.socket.user(someUserId).push({
message: 'single message from server for only current connection',
})
})
After connection user might be identified. Auth logic have to be in special function, that fires after connect and reconnect. User data has to be stored in connection.
User may be identified by an ID, and working with user has to be encapsulated.
user auth:
app.user('user-connected', connection => {
// connection is socket connection instance
// connection.data contains queried creds
let data = connection.data
if ('id' in data) {
db.findUserById(data.id)
.then(res => {
connection.setUser(data.id, res)
// Client got message, that connection is allowed
connection.allow()
})
.catch(err => {
// Client got message, that connection is denied by server
connection.deny({
message: 'this ID not allowed to be authorized'
})
})
}
})
Fire request is the request that not waiting for response. Is just fire and forget.
Example of server middleware for fire.
app.notify(['/user/status/online'], (req, nothing, next) => {
let data = req.body
db.findUserByIdAndUpdate(req.user.id, { status: data.status })
res.send(); // no params
})
Server may have an ability to request client with waiting for response.
User1 invites User2 to chat in realtime:
app.get(['/chat/invite/:userId'], (req, res, next) => {
let userId = req.params.userId
if (req.socket.user(userId)) {
req.socket.user(userId)
.get('/chat/invite')
.then(userRes => {
if (userRes.data === true) {
res.send({
status: true,
charId: 5, // Any generated data after execution
})
}
})
} else {
// user is not online
}
})
Subscription is a possibility for client to join a channel and listen for updates. It may be an object, array or collection. First is need to create an engine, that will make it possible to easily join user to any subscription and then push the data on it with few methods.
Ext: subscription will be like a room in socket.io, that will keep the sync object.
// no example for now
If request was sent to any side, but then this request no longer waits fro answer, requesting side has to signal to responser, to deny answering.
res.send()
called it creates a warning.req.on('canceled', callback)
For current user an instance might be available from req.user
. Any another user has to be available from req.socket.user(userId)
. For unavailable user it has to return undefined
. If no user connection -- user instance has to be destroyed.
User instance has to contain few vars and methods.
interface User {
get id() { } // returns userId
get session() { } // get session data for user
set session() { } // set session data for user
get length() { } // returns a number of user connection in current state.
get connections() { } // returns a list of available connection instances
get(url, body) { } // make get request to client
post(url, body) { } // make post request to client
put(url, body) { } // make put request to client
patch(url, body) { } // make patch request to client
delete(url, body) { } // make delete request to client
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.