Giter VIP home page Giter VIP logo

sockrest's People

Contributors

seitbekir avatar sseidametov avatar valeriitropin avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

valeriitropin

sockrest's Issues

COMMON: Make connections and users container

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.

  • Create EventEmitter based Adapter (use by default)
  • Create RedisEventEmitter based Adapter

Needs to add functions to push messages to clients.

  • Create plugin for client-side routing.

USER: encapsulated user connections

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',
    })
})

USER: auth and identification.

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'
                })
            })
    }
})

Roadmap

  • Support token getting
  • Support user events
  • Create User prototype
  • Support user object to keep connections
  • Make User class to work with connection
  • Add events support
    • first-connected
    • new-connected
    • last-connected
    • user-disconnected

COMMON: Notify request

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
})

COMMON: Server request

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
    }
})

COMMON: Subscription

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

COMMON: Request expired

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.

  • If res.send() called it creates a warning.
  • To handle the request cancel signal use req.on('canceled', callback)

USER: User instance specs

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
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.