FROG icon indicating copy to clipboard operation
FROG copied to clipboard

How It Works: Sending messages through a websocket

Open jkolsen opened this issue 5 years ago • 4 comments

@houshuang This is less of an issue and more of a request for information about how something works. I can move it from here and put it into documentation along with the answer when I have it.

For the websocket connections to students, how does that work? For a session, there is one websocket open and through this, multiple students or groups can connect to the session. When you want to send information to a group that is different from another group, how does this work? Is the information sent to all the groups and then on that side it is filtered or is there something that is done on the sending side that could allow it to just be sent to one group?

jkolsen avatar Oct 10 '19 07:10 jkolsen

You might find this document useful: https://docs.google.com/document/d/1EE44O4mEEU3me94Kxullk22wQp0-QuD7OYOHemd-DcM/edit

Brief answer: Students connect to two websockets. The first websocket is the native Meteor websocket, which is always at the same address as the web server. The other is the ShareDB connection, on localhost it's 3002, in production it's /sharedb, because some firewalls filter connections going to non-standard ports.

The Meteor websocket is responsible for authentication, and for subscribing to/updating collections in MongoDB. Students subscribe to a given session, and we use some custom code in server/main to only send them exactly the activities that should be open for them right now (taking into account any control operators etc). One way we "send a signal" to everyone in a session to for example "pause" is to change data in the session object, which is then sent to everyone who subscribes to that session.

Students open a connection to ShareDB when they access FROG. The moment they open a new activity, they also subscribe to the ShareDB document for that activity instance (it has the id activityId/instance, for example a34/all for p3, a34/group1 for p2, or a34/3312 where 3312 is the userId, for a p1). All coordination in the activity is done by the ActivityRunner updating the shareDb document, and re-rendering whenever someone else updates it.

We have in the past discussed not using Meteor at all to serve FROG to students - just compiling all the Javascript and serving it as a static bundle, and using only ShareDB. Students would then have to subscribe to a "Session" sharedb document which was updated whenever the teacher went to the next activity etc. The main purpose of this is that we thought it would be easier to scale up ShareDB than Meteor.

Not sure what kinds of messages you want to send to students - if you want everyone to know something, modifying the session object is easiest (but it gets sent in full every time its updated, so it should not get too big). The teacher front-end or the server can theoretically load the ShareDB document of any breakout and modify it, but currently we're not doing that. If you wanted to be able to send a message to an individual student, you could create a new Meteor collection called messages, have all the students subscribe, but then have the server filter so that each student just receives the documents with their userId in from/to. This is tricker for groups, since the server doesn't currently keep track of who is in which group at the current moment (well it's obviously somewhere, but the subscription system doesn't have easy access to it), so in that case you could do filtering in the client, as you suggested.

If you have a more specific use case, I might be able to be more specific.

On Thu, Oct 10, 2019 at 9:23 AM jkolsen [email protected] wrote:

@houshuang https://github.com/houshuang This is less of an issue and more of a request for information about how something works. I can move it from here and put it into documentation along with the answer when I have it.

For the websocket connections to students, how does that work? For a session, there is one websocket open and through this, multiple students or groups can connect to the session. When you want to send information to a group that is different from another group, how does this work? Is the information sent to all the groups and then on that side it is filtered or is there something that is done on the sending side that could allow it to just be sent to one group?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/chili-epfl/FROG/issues/1897?email_source=notifications&email_token=AAAPBB543FJHTT2QLVMPSLLQN3J6RA5CNFSM4I7I5AL2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HQ3AHDQ, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAPBB4AEHUX425RIWD6JDTQN3J6RANCNFSM4I7I5ALQ .

-- http://reganmian.net/blog -- Random Stuff that Matters

houshuang avatar Oct 10 '19 07:10 houshuang

Note that it's easy and informative to inspect the two different websocket connections from the Chrome dev panel in the Network tab. 

Screenshot 2019-10-10 at 09 59 44 Screenshot 2019-10-10 at 09 59 39

houshuang avatar Oct 10 '19 08:10 houshuang

Thank you for the info. I do have a more specific use case. Specifically for the dashboards for the robotics project, you had created the sendMsg command. If from a dashboard, the instructor would want to do something like pause the robots for one group, could this be done?

jkolsen avatar Oct 10 '19 08:10 jkolsen

Right, so that's something outside of the mainstream of FROG, and is completely dependent on the specs from Sina which I haven't seen yet. We can do it however he wants. Currently in server/api in that PR (not merged yet) we keep track of the connection and the msgSend command will send a message back on the same websocket. IN that case you are right that each user would receive the same thing, and they could optionally filter locally. There are a hundred ways of doing this, and since nobody else is using this API, it's literally up to Sina to figure out which design makes the most sense - we talked a lot about how to identify different users (should they be identified in the connection string, or just in the payload of the message - might also differ between Cellulo and Thymio, whether all the messages for different users are sent from a single connection, or each user connects independently) etc.

On Thu, Oct 10, 2019 at 10:08 AM jkolsen [email protected] wrote:

Thank you for the info. I do have a more specific use case. Specifically for the dashboards for the robotics project, you had created the sendMsg command. If from a dashboard, the instructor would want to do something like pause the robots for one group, could this be done?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/chili-epfl/FROG/issues/1897?email_source=notifications&email_token=AAAPBB655UQKW2RTGCDB5ELQN3PIBA5CNFSM4I7I5AL2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEA3KLSQ#issuecomment-540452298, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAPBB6J6KEHPB3XGQUFJ2TQN3PIBANCNFSM4I7I5ALQ .

-- http://reganmian.net/blog -- Random Stuff that Matters

houshuang avatar Oct 10 '19 08:10 houshuang