balderdashy / balderdashy/sails
How to subscribe to user events (socket events) without using virtual get (for testing purposes) in sails.js
- Dominant language
- JavaScript
- Stars
- 22.8k
- Forks
- 1.9k
- PR merge metrics
- No merged PRs in 30d
Description
**Node version**: 12.19.1
**Sails version** _(sails)_: 1.4.0
**ORM hook version** _(sails-hook-orm)_: 2.1.1
**Sockets hook version** _(sails-hook-sockets)_: 2.0.0
**Organics hook version** _(sails-hook-organics)_: 0.16.1
**Grunt hook version** _(sails-hook-grunt)_: 4.0.1
I'm trying to implement some tests for my socket and I can't find a way to subscribe to User events without using sails.js virtual GET inside SailsSocket class
the application frontend code looks something like that
import socketIOClient from "socket.io-client";
import sailsIOClient from "sails.io.js";
const io = sailsIOClient(socketIOClient);
io.sails.transports = ['websocket'];
io.sails.url = "http://localhost:1337/";//define the url in http protocol (that's works fine)
io.sails.reconnectionAttempts = 10000;
const socket = io.sails.connect();
socket.get(`/api/v1/socket/subscribe/1`); //virtual GET to the server via the socket object (also fine)
and the backend sails.js code looks like this:
//routes.js
module.exports.routes = {
'GET /api/v1/socket/subscribe/:user_id': {action: 'socket/subscribe'}
}
//socket/subscribe
fn: async function (inputs) {
const id = this.req.param('user_id');
const user = await User.findOne({id});
User.subscribe(this.req, [user.id]);
}
so far so good and everything works pretty well on the application side.
my problem starts when **i'm trying to connect with a different client side code** on a socket class that **don't have virtual GET**
I'm using **k6**, its a load testing tool with some API to connect to sockets.
I'm not sure if virtual GET is part of the web socket protocol or how everything really works around that with sails.js
//frontend code
io.sails.url = "http://localhost:1337/";
//k6 code
var url = 'ws://localhost:1337/socket.io/?__sails_io_sdk_version=1.2.1&__sails_io_sdk_platform=node&__sails_io_sdk_language=javascript&EIO=3&transport=websocket';
var params = { tags: { my_tag: 'hello' } };
var response = ws.connect(url, params, function (socket) {
socket.on('open', function open() {
console.log('connected');
socket.on('message', function (message) {
console.log(`Received message: ${message}`);
});
});
});
I changed the url to use the ws protocol (was originally http) and I managed to connect the socket from **k6, however was not able to receive any user event that published on the backend**
//publish event from the backend to the client
User.publish([1], data);
I can't figure out how to subscribe to those user events outside the client side code inside my application.. (k6 or any other socket library)
thanks for all those that spend the extra time reading all this! :)
some related links:
https://k6.io/docs/javascript-api/k6-ws/socket/
https://sailsjs.com/documentation/reference/web-sockets/socket-client/io-socket-get
https://sailsjs.com/documentation/reference/web-sockets/socket-client/sails-socket
Contributor guide
Research direction
Start with the socket/subscribe action in routes.js and compare its User.subscribe flow with the k6 WebSocket example. Read the linked Sails socket-client references and k6 socket API documentation to establish the expected handshake and subscription behavior. Done means a documented, reproducible way to receive User.publish events from k6 or another non-Sails client.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- backend, networking
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100