Code Monkey home page Code Monkey logo

Comments (7)

nateps avatar nateps commented on September 15, 2024 1

Custom message passing can be implemented on top of the message 'receive' client event / middleware hook:

Server:

backend.use('receive', function(request, next) {
  var data = request.data;
  if (data.myApp) {
    // Handle app specific messages and don't call next
    return;
  }
  // Call next to pass other messages to ShareDB
  next();
});

Client:

connection.on('receive', function(request) {
  var data = request.data;
  if (data.myApp) {
    request.data = null;
    // Handle app specific message
  }
});

Send a message from client:

connection.send({
  myApp: 'messageType',
  ...
});

Send a message from server:

agent.send({
  myApp: 'messageType',
  ...
});

The agent is available in all middleware, so on the server you could send a message in reply to a message from the client, or send a message to the client from the 'connect' middleware as soon as the client connects, for example.

Let me know if that solves your issue!

from sharedb.

RisingGeek avatar RisingGeek commented on September 15, 2024

Is there any way to send a message from the server to all connected clients? I think agent.send will send reply to only 1 client. Am I correct?

from sharedb.

alecgibson avatar alecgibson commented on September 15, 2024

@RisingGeek this doesn't sound related to this issue? What's your use case here?

from sharedb.

RisingGeek avatar RisingGeek commented on September 15, 2024

I am trying to make a real-time editor using sharedb and want to add video calling feature. Using socket io gives errors and when trying to use middleware provided by sharedb, server sends reply to only 1 client because it's ignoring it. I want a listener on client side for custom events.

from sharedb.

alecgibson avatar alecgibson commented on September 15, 2024

I'm not sure I understand what video calling has to do with ShareDB? Do you mean you're trying to send custom messages down the same socket ShareDB is using? In theory it's possible, although probably not advised. Can't you just open up a separate websocket, and send messages using whatever websocket protocol you want?

from sharedb.

RisingGeek avatar RisingGeek commented on September 15, 2024

Yes, I was sending custom message in the same socket that sharedb is using. Thanks for the advice. I will use a separate websocket.

from sharedb.

ajay-wohlig avatar ajay-wohlig commented on September 15, 2024

Custom message passing can be implemented on top of the message 'receive' client event / middleware hook:

Server:

backend.use('receive', function(request, next) {
  var data = request.data;
  if (data.myApp) {
    // Handle app specific messages and don't call next
    return;
  }
  // Call next to pass other messages to ShareDB
  next();
});

Client:

connection.on('receive', function(request) {
  var data = request.data;
  if (data.myApp) {
    request.data = null;
    // Handle app specific message
  }
});

Send a message from client:

connection.send({
  myApp: 'messageType',
  ...
});

Send a message from server:

agent.send({
  myApp: 'messageType',
  ...
});

The agent is available in all middleware, so on the server you could send a message in reply to a message from the client, or send a message to the client from the 'connect' middleware as soon as the client connects, for example.

Let me know if that solves your issue!

I have applied this for passing custom messages but it gives an error Ignoring unrecognized message { while sending message from server

from sharedb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.