ClusterWS / ClusterWS-Client-JS

:fire: JavaScript Client for ClusterWS - lightweight, fast and powerful framework for building scalable WebSocket applications in Node.js.
MIT License
115 stars 10 forks source link

4.0-alpha listen on events from the server #58

Closed caracal7 closed 5 years ago

caracal7 commented 5 years ago

Server

socket.send('myMsg', {
    ID: 123123312,
    message: 'my message'
});

Client

//  Working
socket.on('message', async message => {
    console.log('message', message);
});

//  Not working
socket.on('myMsg', async data => { 
    console.log('data', data);
});
goriunov commented 5 years ago

Basically event on('message') will intercept all messages as it is default websocket event, to process message u need to call inside of event on message socket.processMessage(message); or completely remove on('message') event.

For example

client

socket.on('message', async message => {
    console.log('message', message);
    socket.processMessage(message);
});

socket.on('myMsg', async data => { 
    console.log('data', data);
});

Main reason for that event is that you can set up your own protocol and modify data before passing it to socket.processMessage in correct structure for ClusterWS to understand, or just to ignore / intercept / lookup some messages, also socket.processMessage accepts 2 types of data first is array and second is string with array inside. The same rules apples for Server side.

All of that will be (should be) in documentation hopefully soon :)

caracal7 commented 5 years ago

Thanks for the quick response and for the great library!

With adding socket.processMessage(message); or without socket.on('message',...); everything working perfect!

I'm successfully migrated from 3.x to 4.0.0!