Skip to content

Listen to a channel from any back end file? #4960

Discussion options

You must be logged in to vote

Are those two files run in the same Node.js process? In that case, you could use a simple EventEmitter:

emit.js

import { WebSocket } from 'ws'
import { EventEmitter } from 'node:events';

const ws = new WebSocket('wss://ws.coincap.io/prices?assets=ethereum')

export const myEventBus = new EventEmitter();

ws.on('message', (event) => {
  let data = JSON.parse(event)

  myEventBus.emit('test', data)
})

listen.js

import { myEventBus } from './emit';

myEventBus.on('test', (data) => {
  // ...
});

Reference: https://nodejs.org/api/events.html

If you have two distinct Node.js processes, then you can indeed create a Socket.IO connection:

import { io } from 'socket.io-client'

export const listen =

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@respectabiggle
Comment options

Answer selected by respectabiggle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants