SocketCluster / socketcluster-client

JavaScript client for SocketCluster
MIT License
291 stars 92 forks source link

SocketProtocolError: Socket hung up #89

Closed lautiamkok closed 6 years ago

lautiamkok commented 6 years ago

It does not seem to work when I try to use socketcluster-client & socketcluster-server with Koajs and Nuxtjs. I get this error:

/var/www/html/myproject/node_modules/socketcluster-client/lib/scsocket.js:629
    var err = new SocketProtocolError(SCSocket.errorStatuses[code] || failureMessage, code);
              ^
SocketProtocolError: Socket hung up
    at SCSocket._onSCClose (/var/www/html/myproject/node_modules/socketcluster-client/lib/scsocket.js:629:15)
    at SCTransport.<anonymous> (/var/www/html/myproject/node_modules/socketcluster-client/lib/scsocket.js:285:12)
    at SCTransport.Emitter.emit (/var/www/html/myproject/node_modules/component-emitter/index.js:133:20)
    at SCTransport._onClose (/var/www/html/myproject/node_modules/socketcluster-client/lib/sctransport.js:208:28)
    at WebSocket.wsSocket.onerror (/var/www/html/myproject/node_modules/socketcluster-client/lib/sctransport.js:116:12)
    at WebSocket.onError (/var/www/html/myproject/node_modules/socketcluster-client/node_modules/ws/lib/EventTarget.js:109:16)
    at emitOne (events.js:96:13)
    at WebSocket.emit (events.js:191:7)
    at ClientRequest._req.on (/var/www/html/myproject/node_modules/socketcluster-client/node_modules/ws/lib/WebSocket.js:649:10)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:191:7)
    at Socket.socketErrorListener (_http_client.js:358:9)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:191:7)
    at connectErrorNT (net.js:1028:8)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickDomainCallback (internal/process/next_tick.js:128:9)

What does it mean?

pedronasser commented 6 years ago

I have the same issue here even when trying the Getting started example.

lautiamkok commented 6 years ago

@pedronasser I have got it sorted. Have you?

lautiamkok commented 6 years ago

The reason for this error is because Nuxt is a server side app, while socketcluster-client is to be installed on the client side.

DarkMukke commented 6 years ago

FYI, you can use the client, server-side too. Eg:

const scClient = require('socketcluster-client');
const socket = scClient.connect({hostname: "you.host.name", port: 8008});

although if you do use the client, server-side, i recommend to wrap everything in an on connect event eg:

socket.on('connect', function () {
    console.log('CONNECTED');

    let IssuesChannel = socket.subscribe('Github/Issues');
    IssuesChannel .watch((data) => {
        console.log(data);
    });

});