Setting a Value from non-Effect context

QQ: I'm trying to extract a value from an event listener created from a WebSocket library but I can't seem to figure out how to get that value out of non-Effect land. What's the correct way to do something like this?

import { WebSocket } from 'ws';

// ...snip...

Effect.gen(function* () {
  // Works!
  const socket = yield* Effect.try({
    try: () => new WebSocket(SOCKET_URL),
    catch: () => new Error()
  });

  socket.on('error', console.error);
  socket.on('open', () => {});
  socket.on('message', (data) => {
    const { metadata, payload } = JSON.parse(data.toString('utf8')) ?? {};

    if (metadata?.message_type === 'session_welcome') {
      // Need to update my Ref with payload.session.id here...
    }
  });
})
Was this page helpful?