简体   繁体   中英

not getting updates on model. socket.io with sails.js

I have a sails app created using the options --no-linker --no-front end . The front end of the application is written in angular2 . Making request get and post, seems to work fine.

When i send a get request to the route (to subscribe), i don't get any updates on model being created, updated or destroyed.

I also created a special action, so i could do thing myself, but still with no luck.

The updates performed on the route are made using socket. I don't know where am wrong here. Find my code below

import { Injectable, OnInit, EventEmitter } from '@angular/core'
import { Subject } from 'rxjs/Subject';

import { Donor } from './donor.interface';

import * as socketIO from 'socket.io-client'
import * as sailsIO from 'sails.io'


const url = 'http://localhost:1337'
const io = sailsIO(socketIO)
io.sails.reconnection = true;
io.sails.url = url;

io.socket.on('connect', function () {
    console.log("connected to server")

    io.socket.get('/donor', function (data, jwres) {
        console.log("i subscribed", data, jwres)
    })

    io.socket.get('/donor/hello', function (data, jwres) {
        console.log("i subscribed with hello", data, jwres)
    })

    io.socket.on('donor', function (data) {
        console.log("new donor was created", data)
    });
});

io.socket.on('disconnect', function () {
    console.log('Lost connection to server');
});

DonorController.js

module.exports = {
    hello: function (req, res) {
        if (req.isSocket) {
            Donor.watch(req.socket)
            console.log("new subscriber found")
        } else {
            console.log("not a socket req")
        }

        return res.ok();
    }
};

So i figured out the problem. When you make an update ( CRUD ), the socket performing such operation does not receive an update.

I spent hours before figuring this out. So what i do is act on the data if the CRUD operation is successful like what i would get listing on a model with on

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM