3

I am using loopback to build a route: /users/{id} where users can listen for new information pertaining to their account on their own endpoint. I know I need the socket.io package but I'm not sure what to do with it. How can I open a socket on this dynamic endpoint in the function:

  @get('/users/{id}', {
    responses: {
      '200': {
        description: 'User socket',
        content: {'application/json': {schema: {'x-ts-type': User}}},
      },
    },
  })
  async updateUser(@param.path.string('id') userId: typeof User.prototype.id)
  : Promise<boolean> {
\\ Open socket here
    console.log(userId)
    return true;
  }

if I do this:

const express = require("express");
const app = express();
const server = require("http").createServer(app);
const io = require("socket.io").listen(server);
const port = 3000;

io.on("connection", socket => {
    console.log("User has connected!");
});

It doesn't open a socket on the dynamic endpoint that I want it to.

I am using loopback-4 for the backend and react-native for the front-end.

Vikram Khemlani
  • 555
  • 6
  • 17
  • Please refer to [loopback4-example-websocket](https://github.com/raymondfeng/loopback4-example-websocket) – Zhikai Xiong Oct 24 '19 at 07:37
  • I did, but can't seem to figure out how to integrate it without breaking my original code. I want to keep my rest api and create one route that's dynamic for sockets – Vikram Khemlani Oct 24 '19 at 13:34

0 Answers0