wojtkowiak / meteor-custom-protocol

Send custom data on the Meteor's defaults websocket connection.
MIT License
28 stars 5 forks source link

Message not received on client #11

Open little-brain opened 4 years ago

little-brain commented 4 years ago

Hi, I can't get the client to receive a message from the server. Here's my code:

Meteor.startup(() => {
if (Meteor.isServer) {
        let protocol = JsonProtocol.getInstance();

        protocol.on('myMessage', (data, sessionId) => {
            console.log('server received message', data, sessionId);
            protocol.send('myMessage', { 'myData': `to send from server to ${sessionId}`, sessionId });
        });
    }
});

On the client:

Template.myTemplate.onRendered(function () {
let protocol = JsonProtocol.getInstance();

    console.log('about to send message');
    protocol.send('myMessage', { 'myData': 'to send from client' });

    protocol.on('myMessage', (data) => {
        console.log('client received message', data);
    });
});

The console logs show that the server receives the message from the client, but the client does not receive the message from the server. This looks like a great package if I could only figure out how to get this working? Thanks!

wojtkowiak commented 4 years ago

You have a mistake in the syntax. Sorry, the server part should be throwing in this case.

You have: protocol.send('myMessage', { 'myData': 'to send from server to ${sessionId}', sessionId }); You should have: protocol.send('myMessage', { 'myData': 'to send from server to ${sessionId}' }, sessionId);