I've been trying to figure out how to get realtime updates with a React Native client connected to a Sails.js socket for the last few hours.

I'm not seeing any error messages that I'm aware of.

In the code blocks below, I am sending a request from the React Native functional component discover.js to the Sails server at route user/login, where I subscribe to the User model. Then, I listen to notifications in the client with io.socket.on('user', ...). But when I change the value of one of the users named "Irene," which is the result obtained by the find() ORM method, the io.socket.on('user', ...) event does not fire in the client as expected.

discover.js (A React Native functional component)

import * as React from 'react';
import { useContext, useState, useEffect } from 'react'
import { Text, View, StyleSheet, Animated, Button, TouchableOpacity, Dimensions } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import firebase from './../../firebase.js';
import  AuthContext  from './../imports/AuthContext.js'

var socketIOClient = require('socket.io-client');
var sailsIOClient = require('sails.io.js');

export default function DiscoverScreen() {

    const {user} = useContext(AuthContext)

var io = sailsIOClient(socketIOClient);

// Set some options:
// (you have to specify the host and port of the Sails backend when using this library from Node.js)
io.sails.url = 'http://localhost:1337';

io.socket.on('connect', function onConnect(){


    io.socket.post('/user/login', {uid: user.uid} , function serverResponded (body, JWR) {
      // body === JWR.body
      console.log('Sails responded with: ', body);
      console.log('with headers: ', JWR.headers);
      console.log('and with status code: ', JWR.statusCode);

      if (JWR.error) {
        console.error('Could not subscribe to notifications for users named Irene: '+JWR.error);
        return;
      }
    });

    io.socket.on('user', function (msg) {
        console.log("SOCKET CLIENT NOTIFICATION")
        console.log(msg)
    });
});

login.js (Sails.js controller file - /api/controllers/user/login.js

module.exports = async function login(req, res) {

    if (!req.isSocket) {
      return res.badRequest();
    }

      
    try {
        var record = await User.find({
            firstName: "Irene"
        })

    } catch (err) {
        sails.log.debug(err)
    }

  User.subscribe(req.socket, _.pluck(record, 'id'));

  return res.ok(record);

};

User.js (Sails.js model file - /api/models/User.js)

module.exports = {
  primaryKey: 'id',
  attributes: {
    uid: {
      type: 'string'
    },
    birthday: {
      type: 'string',
      columnType: 'date'
    },
    city: {
      type: 'string'
    },
    college: {
      type: 'string'
    },
    employer: {
      type: 'string'
    },
    firstName: {
      type: 'string'
    },
    fullName: {
      type: 'string'
    },
    gender: {
      type: 'string'
    },
    heightFeet: {
      type: 'number'
    },
    heightInches: {
      type: 'number'
    },
    job: {
      type: 'string'
    },
    lastName: {
      type: 'string'
    },
    religion: {
      type: 'string',
      isIn: [
        'Buddhist',
        'Christian',
        'Catholic',
        'Spiritual but not religious',
        'Neither religious nor spiritual',
        'Other'
      ]
    },
    preferences: {
      model:'preference',
      columnName: 'preference_id'
    }
  },
  tableName: 'users'
}

I've tried a lot of different things. What I know is working/probably not the source of the problem:

  1. The expected rows are being selected by the find() method. There are three users whose first name is Irene and the findOne is getting these users.
  2. The _.pick method is correctly returning the primary keys of these users [1, 5, 30].
  3. The primary key of the users table is id and this is indicated as such in the model file User.js
  4. The io.socket.on('user', ...) can be called even before the socket connection is made because the documentation said that events attached before connecting are placed in a queue and run when the connection happens. So I don't think the lack of connection is the issue here.
  5. The three users named Irene are being returned in the callback function serverResponded's parameter "body."
  6. I tried setting the first argument of User.subscribe to both req.socket (as is shown in the code above) and simply req, and neither worked.

Help would be very much appreciated.

0

There are 0 answers