pusher / pusher-websocket-react-native

React Native official Pusher SDK
MIT License
62 stars 55 forks source link

Problem with running on simulator and device arm64 vs x86 #25

Open EwaGuziejko opened 2 years ago

EwaGuziejko commented 2 years ago

Hello, I am trying to add Pusher ( v 1.0.2) to my existing react native project (react native version 0.64.1). Though I have trouble doing so. First I get an error when trying to run it on my simulators node_modules/@pusher/pusher-websocket-react-native/ios/PusherWebsocketReactNative.swift:1:8: error: could not find module 'PusherSwift' for target 'arm64-apple-ios-simulator'; found: x86_64-apple-ios-simulator, x86_64,. When I try to run it on the real device I get multiple errors Undefined symbols for architecture arm64: and then a whole list of methods that have this problem. I am using XCode Version 13.0 (13A233). Does anybody know what might cause this issue? I am not running this code on a device with an M1 processor.

proggen-com commented 2 years ago

Did you run:

pod install

in the ios directory?

EwaGuziejko commented 2 years ago

Thanks for your response. Yes, I did, it didn't help, and neither did this lib work for android. I ended with having to upgrade my react native version and it did help. So to sum up I think that it's incompatible with rn versions < 0.67

smalik02 commented 1 year ago

I'm running into this issue now - installing on an M1 device.

here is the error message: could not find module 'PusherSwift' for target 'arm64-apple-ios-simulator'; found: x86_64-apple-ios-simulator, at: ...ekxwrkuxftiwkscxidomgaxzgxne/Build/Products/Debug-iphonesimulator/PusherSwift/PusherSwift.framework/Modules/PusherSwift.swiftmodule

Xcode version: 14.3 react-native: 0.70.5

MursiDirect commented 1 year ago

I have exactly the same issue as yours, did you find a solution to this issue? @smalik02

sokolovdm commented 4 months ago

I have the same issue too. Any solutions?

sokolovdm commented 4 months ago

I found a solution via native web socket

import { useMemo, useRef } from 'react'

import { deepParseJson } from 'deep-parse-json'

import { useDestroyed, useUpdate } from '@/packages/hooks'
import { getEnumKey, getTime } from '@/packages/utils/other'

import { selectors } from '@/app/model'
import {
    SocketEmitDefault,
    SocketEvent,
    SocketEventDefault,
    SocketEventResponse,
} from '@/app/socket/types'

import authModel from '@/features/auth/model'

import { viewerModel } from '@/entities/viewer/model'
import {
    onBusinessCardStatusChanged,
    onChangedAccountStatus,
    onGotNewAccount,
    onGotNewAccountCard,
    onGotNewEmployee,
} from '@/entities/userAccount/model/socket'

import socketApi from '@/shared/api/socket'
import { AuthStatus } from '@/shared/lib/types/helpers'
import authStorage from '@/shared/lib/storage/auth'

import env from '@/env'

export const useSocket = () => {
    const socket = useRef<Maybe<WebSocket>>(null)

    const { viewer } = viewerModel.useViewer()
    const { data } = authModel.useAuthStatus()

    const isInit = selectors.useAppInitialized()

    const isAuth = useMemo(
        () => [AuthStatus.AUTHORIZED, AuthStatus.UN_AUTHORIZED].includes(data),
        [data],
    )

    const isCanInitSocket = useMemo(
        () =>
            Boolean(isInit && isAuth && !socket.current && Boolean(viewer.id)),
        [isInit, isAuth, socket, viewer.id],
    )

    const isNeedDestroySocket = useMemo(
        () => !isAuth && Boolean(socket.current),
        [isAuth],
    )

    useUpdate(() => {
        if (isCanInitSocket) {
            onInitSocket().then()
        }
    }, [isCanInitSocket])

    useUpdate(() => {
        if (isNeedDestroySocket) {
            destroySocket()
        }
    }, [isNeedDestroySocket])

    const destroySocket = () => {
        socket.current?.close()
        socket.current = null
    }

    useDestroyed(destroySocket)

    const onInitSocket = async () => {
        const token = await authStorage.getToken()
        if (token) {
            const newSocket = new WebSocket(
                `wss://ws-${env.SOCKET_CLUSTER}.pusher.com/app/${env.SOCKET_KEY}?protocol=7&client=js&version=4.3&flash=false`,
                [],
                {
                    headers: {
                        token: `Bearer: ${token}`,
                    },
                },
            )

            newSocket.onmessage = e => {
                console.log(e, 'SOCKET EVENT')
                const parsedEventData = deepParseJson(
                    e.data,
                ) as ResponseSocketEventDefault<any>['data']

                const event = parsedEventData.event

                const eventName = getEnumKey(
                    { ...SocketEventDefault, ...SocketEvent },
                    event,
                )

                switch (event) {
                    case SocketEventDefault.GET_SOCKET_ID:
                        initSocket({
                            data: parsedEventData,
                            newSocket,
                        })
                        break
                    ...your events
                    default:
                        break
                }
            }

            newSocket.onerror = e => handleError(e)
        }
    }

    const initSocket = async (payload: {
        data: ResponseSocketEventDefault['data']
        newSocket: WebSocket
    }) => {
        try {
            socket.current = payload.newSocket

            const eventData = payload.data
                .data as unknown as SocketEventResponse.GotConnectId['data']['data']
            const response = await socketApi.getSocketId({
                socket_id: eventData.socket_id,
                channel_name: `common.${viewer.id}`,
            })

            const emitData = {
                auth: response.auth,
                channel: `common.${viewer.id}`,
            }

            socket.current?.send(
                JSON.stringify({
                    event: SocketEmitDefault.SUBSCRIBE,
                    data: emitData,
                }),
            )
        } catch (e) {
            destroySocket()
            onInitSocket()
        }
    }

    const handleError = (error: WebSocketErrorEvent) => {
        console.log('Socket error:', error)
    }

    return {
        socket,
        onInitSocket,
    }
}
endruuu commented 3 months ago

Temporary solution is to use x86_64 architecture when building, e.g.

arch -x86_64 npx expo run:ios