简体   繁体   中英

Making a connection with Socket.io on Remote Server

I have created a simple app Laravel Homestead to test some basic stuff: Broadcasting events and listening them on socket.io.

Everything works well.

However, I want to upload the app to a remote server to a directory called learn-redis and am facing a challenge. My socket.js file looks like this:

var server = require('http').Server();

var io = require('socket.io')(server);

var Redis = require('ioredis');

var redis = new Redis();

redis.subscribe('test-channel');

redis.on('message', function(channel, message){

    message = JSON.parse(message);

    io.emit(channel + ':' message.event, message.data);

});

server.listen(8000); 
  1. In Laravel Homestead, I know I can listen to port 8000 because it gets displays on the console when running vagrant up . What about my remote server? What port do I use?

  2. On client side, I have this code.

    var socket = io(' http://192.168.10.10:8000 ');

      new Vue({ el: '#demo', data: { users: {!! App\\User::all() !!} }, ready: function(){ socket.on('test-channel:App\\\\Events\\\\UserSignedUp', function(data) { this.users.push( data.user ); }.bind(this)); } }); 

I have pulled Vue.js and Socket.io.js libraries, but when I try doing var socket = io('http://example.com/learn-redis:8000') I get the error http://example.com:8000/socket.io/?EIO=3&transport=polling&t=LBWnq-U net::ERR_CONNECTION_TIMED_OUT .

You need to have a VPS server to be able to install node,most likely, You are on a shared hosting and most providers won't allow you to install node. Your best bet would be to ask your provider to install node for you or if you have the funds, upgrade to a VPS server.

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