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

Lost connection to server #24

Open
adekunle11 opened this issue May 10, 2021 · 1 comment
Open

Lost connection to server #24

adekunle11 opened this issue May 10, 2021 · 1 comment

Comments

@adekunle11
Copy link

Hello, I'm trying to use peerjs & webrtc to create a video chat app.
I have this code in my client

export const API_URI = 'https://mvmserver.herokuapp.com/'


// Peer Config

const peerServer = new Peer(1, {
    host: 'mvmserver.herokuapp.com',
    secure: false,
    port: 443,
    path: '/mypeer'
})

peerServer.on('error', console.log)
// Socket config

export const socket = IO(`${API_URI}`, {
    forceNew: true
})

socket.on('connection', () => console.log('Connected client'))

export const joinRoom = (stream) => async (dispatch) => {
    const roomID = 'chat1_user1'
    //set my own stream
    dispatch({type: MY_STREAM, payload: stream})

    //Open a connection to our server
    peerServer.on('open', (userId) => {
        socket.emit('join-room', {userId, roomID})
    })

    socket.on('user-connected', (userId) => {
        connectToNewUser(userId, stream, dispatch)
    })

    // Receive a call
    peerServer.on('call', (call) => {
        call.answer(stream)

        //stream back the call

        call.on('stream', (stream) => {
            dispatch({type: ADD_STREAM, payload: stream})
        })
    })
};


function connectToNewUser(userId, stream, dispatch) {

    const call = peerServer.call(userId, stream);
    call.on('stream', (remoteVideoStream) => {
        if (remoteVideoStream) {
            dispatch({type: ADD_REMOTE_STREAM, payload: remoteVideoStream})
        }
    })

}

and this in my heroku server

const express = require('express')
const http = require('http')
const socketio = require('socket.io')

const {ExpressPeerServer} = require('peer');

const app = express();

const server = http.createServer(app)

const io = socketio(server).sockets

//Borderparser
app.use(express.json())

const customGenerationFunction = () =>     (Math.random().toString(36) + "0000000000000000000").substr(2, 16)

const peerServer = ExpressPeerServer(server, {
    debug: false,
    secure: false,
    path: '/',
    genderateClientId: customGenerationFunction
})

io.on('connection', function(socket) {
    console.log('connected')
    socket.on('join-room', ({roomID, userId}) => {
        socket.join(roomID)
        socket.to(roomID).broadcast.emit('user-connected', userId)
    })
})

const port = process.env.PORT || 443

server.listen(port, () => console.log(`Server is running on port ${port}`))

But i get the error, lost connection to server everytime my app launches. what could be wrong

@adekunle11
Copy link
Author

fixed, i needed to add iceServers and also, some versions of socket.io don't work well, so i downgraded to 2,1.1

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