Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use more than one client to access the methods in a route #11

Open
opensourcegeek opened this issue Oct 7, 2015 · 0 comments
Open

Comments

@opensourcegeek
Copy link

In the below code (WebsocketAPI -> getDevices) when I emit the payload(res.socket.emit(payload)) it works fine when I have just one client attached. When I have another client, only one of them get the payload. I tried invoking emit method on req.socket and res.socket both seem to play up.

When I use res.json(payload) all the clients attached get the data. Since getDevices method is invoked when a client initially connects to the server, any time a new client gets connected to server, the server pushes the payload to all the clients attached. I'm not sure where I'm going wrong. Thanks

// actual API implementation
export class WebsocketAPI {
    constructor(dbService, db) {
        console.log(this.onData);
        this.api = new APIInterface(dbService,
            db,
            this.onData.bind(this));
        this.connectedClients = {};
    }

    getDevices(req, res) {
        return this.api.getDevices().then((payload) => {
            //req.io.respond(payload);
            console.log(req.socket);
            //req.socket.emit(payload);
            res.socket.emit(payload);
        });
    }
}


// setting up routes
export function websocketSetup(app, dbService, db) {
    let webSocketAPI = new WebsocketAPI(dbService, db);
    console.log('Setting up websocket');

    app.io.route('ax-devices', {
        getAll: (req, res) => {
            console.log('getAll called');
            return webSocketAPI.getDevices(req, res);
        }
    });

    app.io.route(constants.AX_DEVICE_WRITE, (req, res) => {
        console.log('Write called');
        return webSocketAPI.write(req, res);
    });

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant