SocketCluster / socketcluster

Highly scalable realtime pub/sub and RPC framework
https://socketcluster.io
MIT License
6.15k stars 314 forks source link

"#1" every 10 seconds #494

Closed davidmoshal closed 5 years ago

davidmoshal commented 5 years ago

Hi, have been testing out SC. Created a default SocketCluster template app. Then created this client:

socketCluster.create({port: 80})
    .on('message', msg => console.log(msg))

Received the following output. which prevented me from parsing all messages as JSON (I'm sending all messages as JSON):

{"rid":1,"data":{"id":"4T6IyuoC2dO5DQVyAAAD","pingTimeout":20000,"isAuthenticated":false}}
#1
#1
#1
... the #1 repeats every 10 seconds

Am wondering what this is from, and where it's documented? Also wondering what other messages can be expected?

davidmoshal commented 5 years ago

ok, found it in the docs: https://github.com/SocketCluster/socketcluster/blob/master/socketcluster-protocol.md

For the ping/pong protocol, the SocketCluster server will periodically send a ping message to the client and the client has to answer every ping with a pong message.

The ping message which comes from the server is just the string:

#1

Whenever you get this message from the WebSocket, your client needs to send back this string as soon as possible:

#2

The strings #1 and #2 were chosen because they are relatively short (compared to sending JSON objects). Note that there are three kinds of strings which have special meaning within SocketCluster:

Ping/pong strings (I.e. #1 and #2)
Event packets (I.e. JSON objects that have an event property)
Response packets (I.e. JSON objects that have an rid property)

However, before I close this, there are still questions: 1) in my example above, did I need to manually respond with '#2', or is that handled implicitly for me? 2) is the interval between '#1's the 'pingInterval' (ie: default 8 seconds)?

chorsnell commented 5 years ago

Hi @davidmoshal if you are using the socketcluster-client then this is handled for you. If you are using it with native websockets then you will need to handle the ping response and send back a #2 as you mention, otherwise the server / worker will close the connection after the timeout period.

Where possible I would advise to use the socketcluster-client, as it works in nodejs and the browser, plus has various other client libraries. I'm actually on a voyage of potentially creating my own, as the client doesn't work in nativescript

davidmoshal commented 5 years ago

@poppahorse thanks, yes am using socketcluster-client. Good to know that I could use native WS and implement the acks.

Actually I'm also using socketcluster-server because it's simpler to setup typescript with intellij breakpoint debugging with socketcluster-server (one typescript file) than using the whole server stack created by the template app. Might switch back to the full stack if I need clustering and better failover.

Thanks again.