question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Websocket Invalid frame header Error with local PeerJS Server

See original GitHub issue

Hi,

I succeed in using PeerJS with cloud server to make video conference thing, along with the websocket library ws. Now I tried to setup a local PeerJS Server, following the instruction from “Combining with existing express app” here, but It didn’t work and things acted weirdly. Below are my codes:

– Server –

//SET_UP
var express = require('express');
var app = express();
var ExpressPeerServer = require('peer').ExpressPeerServer;
var options = {
    debug: true
};
var http = require('http');
var server = http.createServer(app);
var port = process.env.PORT || 9000;

app.get('*', function(req, res){
    res.sendFile(__dirname + req.url);
});
app.use('/peerjs', ExpressPeerServer(server, options));

server.listen(port);

//Websocket
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer( {'server': server} );

wss.on('connection', function(ws){
    ws.on('message', function(data){
        // ...
    });

    ws.on('close', function(){
        // ...
    });

});

– Client –

peer = new Peer('123abc', { host: 'localhost', port: 9000, path:'/peerjs', debug:true});

// Get an ID from the PeerJS server     
peer.on('open', function(id) {
    console.log('My peer ID is: ' + id);

    // Then connect to the socket server                        
    connectSocket();
});

And this is what I got when I open webpage http://localhost:9000/index.html.

PeerJS:  Socket open
WebSocket connection to 'ws://localhost:9000/peerjs/peerjs?key=peerjs&id=123abc&token=i1nay3ueh3tprpb9' failed: Invalid frame header
PeerJS:  Socket closed.
PeerJS:  ERROR Error: Lost connection to server.

It seems the PeerJS socket was connected once but shortly disconnect after. I guess it’s something with localhost but I don’t know what’s wrong. And it still happened if though I comment out my websocket server. Couldn’t figure out why. Please advice with any ideas and feedback. Thanks!

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5

github_iconTop GitHub Comments

5reactions
jhclauracommented, Jul 21, 2015

Solved it! Should create two servers for each websocket with different ports. My code might not be efficient, but it works.

var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var port = process.env.PORT || 7000;

// peer_server
var ExpressPeerServer = require('peer').ExpressPeerServer;
var peerExpress = require('express');
var peerApp = peerExpress();
var peerServer = require('http').createServer(peerApp);
var options = { debug: true }
var peerPort = 9000;

app.get('*', function(req, res){
    res.sendFile(__dirname + req.url);
});
peerApp.use('/pp', ExpressPeerServer(peerServer, options));

server.listen(port);
peerServer.listen(peerPort);
1reaction
Maykonncommented, Feb 4, 2016

Thanks for your reply. My problem is another: WSS connection not working.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Websocket failed Invalid frame header - node.js - Stack Overflow
Everything works fine on localhost. but when i push/host it on heroku it shows me this error in the console of browser :...
Read more >
Local PeerJS Server disconnected after connected, and then ...
WebSocket connection to 'ws://localhost:9000/peerjs/peerjs?key=peerjs&id=123abc&token=i1nay3ueh3tprpb9' failed: Invalid frame header. PeerJS: Socket closed.
Read more >
Using WebSockets on Heroku with Node.js
We'll develop a simple application that shares the server's current time with the client via a persistent socket connection. Each application ...
Read more >
Reason: CORS request did not succeed - HTTP | MDN
The HTTP request which makes use of CORS failed because the HTTP connection failed at either the network or protocol level. The error...
Read more >
WebRTC 1.0: Real-Time Communication Between Browsers
If the content of description is not valid SDP syntax, then reject p with an RTCError (with errorDetail set to " sdp-syntax-error "...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found