Wednesday, 19 April 2017

React Native: How to integrate Socket.io with Sails.js?

I have the following, a simple server and client connection with Express using NodeJS:

var express = require('express');
var http = require('http')
var socketio = require('socket.io');

var app = express();
var server = http.Server(app);
var websocket = socketio(server);
server.listen(3000, () => console.log('Listening to: 3000'));

// The event will be called when a client is connected.
websocket.on('connection', (socket) => {
  console.log('Client just joined with ', socket.id);
});

and:

import React from 'react';
import SocketIOClient from 'socket.io-client';

class Main extends React.Component {
  constructor(props) {
    super(props);

    // Creating the socket-client instance will automatically connect to the server.
    this.socket = SocketIOClient('http://localhost:3000');
  }
}

module.exports = Main;

Would simply want a chat room between two people, and a list of those chat rooms with persistent messages. But I’ve integrated the project with Sails.js, and was wondering how I should handle it with Socket.io?

Thank you in advance and will be sure to vote up/accept answer



via Jo Ko

No comments:

Post a Comment