randy
randy

Reputation: 166

How do I catch ping/pong frames sent to my ws listener from the server?

The ws package for nodeJS hides incoming ping frames by default and silently responds to them with pong frames.

How can I catch these incoming ping frames and take note of them?

Upvotes: 2

Views: 3085

Answers (3)

Ken Lin
Ken Lin

Reputation: 1919

Per https://github.com/websockets/ws#how-to-detect-and-close-broken-connections

client.on('ping', heartbeat);

Upvotes: 0

randy
randy

Reputation: 166

You just listen for a ping event: https://github.com/websockets/ws/blob/master/doc/ws.md#event-ping

The real answer here is RTFM.

Upvotes: 1

IvanD
IvanD

Reputation: 2933

You need a Node app for that. The app and your front-end (FE) will have open websockets via which they will communicate.

Conceptually, you run a node server and open a web-socket on it. Then you serve your FE to users. The FE in user's browser opens connection back to the server via the websocket. The server sends/pushes some messages to FE via this open channel, and also the client can send some messages to the app.

The websockets differ from a simple requests in that you can PUSH data to the FE. With simple requests, the FE can only PULL data from the server.

Upvotes: -1

Related Questions