Vasiliy Belov
Vasiliy Belov

Reputation: 11

How to define custom rooms for WebSocket in sails.js?

Now in default sails.js 0.12.3 I can use Model.PublicCreate only for predifened models...

So I want to send messages to rooms that are not models (about 40 rooms).. How to do that with sails.js?

Upvotes: 1

Views: 450

Answers (1)

hlozancic
hlozancic

Reputation: 1499

You need to join socket to your custom room first. So for example make socket route where user will join to custom room and add this line of code somewhere in your controller:

sails.sockets.join(req, 'myCustomRoomName');

You can add socket to any number of rooms. Check this docs for more info about that: http://sailsjs.org/documentation/concepts/realtime/on-the-server

When you want to send message to all sockets connected to myCustomRoomName use this:

sails.sockets.broadcast("myCustomRoomName", "nameOfEvent", {someData: "can also be just string instead of obj, i prefer objects..."});

More info about this topic: http://sailsjs.org/documentation/reference/web-sockets/sails-sockets

Upvotes: 1

Related Questions